Thread is a very important concept in every computer language,for example in java,in c++ or in c.So Ruby have its own Thread concept.
Create a new Thread
If you want to create a new thread in java,there are two ways(first,you can write a class which extend the java.lang.Thread class;second,you can implement the java.lang.Runable interface).But in ruby,there is only one way to create a thread.Because you can pass a block into every function,you shouldn't write your own class to extend a class or include a module,you only need write a block which contains all codes you want to do and pass it into the new method of Thread.The following is a sample:
t1 = Thread.new { puts "Create a new Thread"}
There are some differences between java and ruby.After you create a Thread instance,you need call the run method of the Thread instance to run the thread in Java.But in ruby,when you call the new method,you start the thread which you create.Second,you can get a return value from a thread,(The thread should return the last expression in the block passed into Thread.new method).You can use the value attribute to access the return value.
Manage the Thread
As the same in java,you can control a thread with some methods of the Thread class.You can control a thread in ruby,too.The following is a list of these methods which can control a thread:
kill
pass:like the wait method in java
stop:set the current thread in sleep status.The defference between stop and
sleep is that there is not a sleep time parameter which should be passed to the stop method.
exit:
join:
priority=:set a thread the priority value.
raise:throw an exception in another exception
run:like notify(?I have some doubt about it)
terminate:
wakeup:
[/list]
There are some methods to access a thread's status:
main:return the main thread in this process(There is not a same method in java)
alive?
stop?
status
Share thread variable
You can set some variables in thread and then you can access these variables in other threads if they have the reference of the thread.This usage like the ThreadLocal class in java,but they have many differences.
thread = Thread.new do
t = Thread.current
t[:var1] = "This is a string"
t[:var2] = 365
end
# Access the thread-local data from outside...
x = thread[:var1] # "This is a string"
y = thread[:var2] # 365
has_var2 = thread.key?("var2") # true
has_var3 = thread.key?("var3") # false
分享到:
相关推荐
本书《Making Use of Ruby》由Suresh Mahadevan撰写,并由Wiley Publishing, Inc.出版,旨在为读者提供全面深入的Ruby语言学习资源。本书不仅适合Ruby初学者作为入门指南,也适合有一定经验的开发者进一步提升自己的...
Ruby语言 1 Grant Ren 1 第一部分 Ruby语言基础 8 第一章 Ruby语言概述 8 §1.1 Ruby的历史 8 §1.2 Ruby名字的由来 8 §1.3 Ruby的特点 8 §1.4 Ruby和Python的比较 9 第二章 Ruby编程环境 9 §2.1 Ruby的安装 9 §...
- **线程创建**:使用`Thread.new`创建新线程。 - **线程操作**:线程可以被暂停、唤醒、等待结束等。 - **线程和异常**:异常会在线程中传播。 - **线程调度**:操作系统负责线程的调度。 - **线程同步**: - **...
### Ruby程序设计(简洁清新的教程) #### 一、Ruby语言概述 **1.1 Ruby的历史** Ruby语言是由日本人松本行弘(Matsumoto Yukihiro)在1993年开始开发的一种脚本语言。起初,Matsumoto对脚本语言产生了浓厚的兴趣,...
- **线程(Thread)**:Ruby的线程模型,包括线程安全的问题和解决方法。 - **进程(Process)**:创建子进程的方式及其应用场景。 #### 8. **网络编程** - **客户端与服务器**:构建网络应用程序的基本原理。 - **...
for i in 1..5 puts i end ``` #### 二、面向对象编程(OOP) **2.1 类和对象** 类是对象的模板,可以定义对象的行为和属性。例如: ```ruby class Car def initialize(model, year) @model = model @...
Ruby的作业调度程序(at,cron,in和每个作业)。 它使用线程。 注意:也许您正在寻找的? (特别是如果你使用这是上鲁弗斯调度2.0.24) 快速开始: # quickstart.rb require 'rufus-scheduler' scheduler = ...
- **10.1.1 线程创建**:使用`Thread.new`创建新线程。 - **10.1.2 线程操作**:包括启动、等待结束等。 - **10.1.3 线程和异常**:如何处理线程中发生的异常。 - **10.1.4 线程调度**:控制线程的执行顺序。 - **...
Thread平台的AIOT智能语音管家设计源码,包含2891个文件,包括1077个头文件、1025个C源文件、189个HTML文件、125个S文件、41个文本文件、29个Python脚本、18个Markdown文件、14个PNG图像文件、9个Shell脚本、8个IN...
要求在不受支持的Ruby上执行此操作将导致无操作用法require 'debug_inspector'# Open debug context# Passed `dc' is only active in a blockRubyVM :: DebugInspector . open { | dc | # backtrace locations ...
- fully supports fork, can detect fork in various ways and automatically re-arms kernel mechanisms that do not support fork. - highly optimised select, poll, epoll, kqueue and event ports backends. -...
1. **错误和异常处理**:在Ruby中,使用`begin-rescue-end`块来捕获和处理可能出现的异常。理解如何正确地捕获和记录错误,以及如何设计健壮的错误处理逻辑,是测试面试中的关键部分。 2. **调试技巧**:使用Ruby的...
1、threads-max (/proc/sys/kernel/threads_max) 这个值表示物理内存决定的系统进程数上限,fork_init中有: max_threads = mempages / (THREAD_SIZE/PAGE_SIZE) / 8 2、pid_max (/proc/sys/kernel/pid_max) 这个值...
at=info thread_id=70351652783840 process_id=55394 request_id=013f9cc29c1e4c483435dbc15ab260f4 measure#pre_request=0ms measure#rack_in=202ms measure#app=505ms measure#rack_out=301ms 包括: thread_id ...
1. **即时编译器(Just-In-Time Compiler, JIT)** - JIT编译器能够将热点代码(频繁调用的方法或循环体)编译成本地机器代码,以提高程序的执行效率。 - HotSpot VM中的C1和C2编译器分别针对简单代码优化和复杂代码...
<li><a href="/ruby">Ruby</a></li> ''' handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element ...