`
reverocean
  • 浏览: 196126 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Thread in Ruby(1)

    博客分类:
  • Ruby
阅读更多
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
分享到:
评论
1 楼 reverocean 2007-12-12  
I almost lost this blog just now.Fortunately,I found it from the browser's history

相关推荐

    Making Use of Ruby

    本书《Making Use of Ruby》由Suresh Mahadevan撰写,并由Wiley Publishing, Inc.出版,旨在为读者提供全面深入的Ruby语言学习资源。本书不仅适合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 §...

    RUBY基础入门指南

    - **线程创建**:使用`Thread.new`创建新线程。 - **线程操作**:线程可以被暂停、唤醒、等待结束等。 - **线程和异常**:异常会在线程中传播。 - **线程调度**:操作系统负责线程的调度。 - **线程同步**: - **...

    Ruby程序设计(简洁清新的教程)

    ### Ruby程序设计(简洁清新的教程) #### 一、Ruby语言概述 **1.1 Ruby的历史** Ruby语言是由日本人松本行弘(Matsumoto Yukihiro)在1993年开始开发的一种脚本语言。起初,Matsumoto对脚本语言产生了浓厚的兴趣,...

    Ruby Developer's Guide

    - **线程(Thread)**:Ruby的线程模型,包括线程安全的问题和解决方法。 - **进程(Process)**:创建子进程的方式及其应用场景。 #### 8. **网络编程** - **客户端与服务器**:构建网络应用程序的基本原理。 - **...

    以下是一个非常详细的Ruby语言教程.docx

    for i in 1..5 puts i end ``` #### 二、面向对象编程(OOP) **2.1 类和对象** 类是对象的模板,可以定义对象的行为和属性。例如: ```ruby class Car def initialize(model, year) @model = model @...

    rufus-scheduler:Ruby的调度程序(at,in,cron和每个作业)

    Ruby的作业调度程序(at,cron,in和每个作业)。 它使用线程。 注意:也许您正在寻找的? (特别是如果你使用这是上鲁弗斯调度2.0.24) 快速开始: # quickstart.rb require 'rufus-scheduler' scheduler = ...

    Ruby入门教程

    - **10.1.1 线程创建**:使用`Thread.new`创建新线程。 - **10.1.2 线程操作**:包括启动、等待结束等。 - **10.1.3 线程和异常**:如何处理线程中发生的异常。 - **10.1.4 线程调度**:控制线程的执行顺序。 - **...

    基于RT-Thread平台的AIOT智能语音管家设计源码及跨语言实现细节

    Thread平台的AIOT智能语音管家设计源码,包含2891个文件,包括1077个头文件、1025个C源文件、189个HTML文件、125个S文件、41个文本文件、29个Python脚本、18个Markdown文件、14个PNG图像文件、9个Shell脚本、8个IN...

    debug_inspector:MRI 2.0 debug_inspector API的Ruby包装器

    要求在不受支持的Ruby上执行此操作将导致无操作用法require 'debug_inspector'# Open debug context# Passed `dc' is only active in a blockRubyVM :: DebugInspector . open { | dc | # backtrace locations ...

    C事件驱动循环的网络IO编程框架的源码.rar

    - 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. -...

    rootins-problem:在根本保险中测试面试过程中的问题

    1. **错误和异常处理**:在Ruby中,使用`begin-rescue-end`块来捕获和处理可能出现的异常。理解如何正确地捕获和记录错误,以及如何设计健壮的错误处理逻辑,是测试面试中的关键部分。 2. **调试技巧**:使用Ruby的...

    linux 进程数最大值修改方法

    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) 这个值...

    rack-timing:Rails 应用程序机架堆栈的此类时间戳。 哇

    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 ...

    The Java Virtual Machine Specification 3rd

    1. **即时编译器(Just-In-Time Compiler, JIT)** - JIT编译器能够将热点代码(频繁调用的方法或循环体)编译成本地机器代码,以提高程序的执行效率。 - HotSpot VM中的C1和C2编译器分别针对简单代码优化和复杂代码...

    Python api 库函数学习

    <li><a href="/ruby">Ruby</a></li> ''' handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element ...

Global site tag (gtag.js) - Google Analytics