在Ruby中线程是用户级线程并依赖与操作系统。线程是进程中的一个实体,是被系统独立调度和分配的基本单位。线程可以与同属于同样个进程的其他线程共同共享进程的全部资源。但是线程不拥有资源,只需要一点在运行时必不可少的资源。
线程的基本控制,如果主进程结束,会结束所有的线程。所以如果你的线程在执行显示的任务,但是主进程却已经执行完毕,会看不到线程的任何输出。如链接中所示:
http://blog.csdn.net/weixingstudio/article/details/7796442
线程的创建可以使用Thread.new,同样可以以同样的语法使用Thread.start 或者Thread.fork这三个方法来创建线程。
创建线程并执行的基本代码:
- i=1
- puts "hello thread"
- puts Time.new
- #round=5
- #while i<round
- # puts "the #{i}th round"
- # i=i+1
- #end
- thread1=Thread.start 10 do |value|
- while i<value
- puts "#{i}"
- i=i+1
- end
- end
- thread1.join
- thread2=Thread.start do
- 10.times do |a|
- puts "the #{a+1} output"
- end
- end
- thread2.join
上面的代码中,线程thread1线程创建以后,调用了thread1.join方法,这个方法会挂起主线程,等待thread1线程完成,这样就可一在主线程结束之前看到线程thread1,thread2的输出消息。
上面的线程的运行结果:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- the 1 output
- the 2 output
- the 3 output
- the 4 output
- the 5 output
- the 6 output
- the 7 output
- the 8 output
- the 9 output
- the 10 output
但是这样可以看到,线程的输出是等到线程thread1完全执行完毕以后才执行线程thread2,有经验的朋友就会知道线程的调度是随机的,所以如果没有thread1.join和thread2.join两个方法,线程的输出就会变得没有规律,下面我们该一下程序看看没有规律的线程输出。
- i=1
- puts "hello thread"
- puts Time.new
- #round=5
- #while i<round
- # puts "the #{i}th round"
- # i=i+1
- #end
- thread1=Thread.start 10 do |value|
- while i<value
- puts "#{i}\n"
- i=i+1
- end
- end
- thread2=Thread.start do
- 10.times do |a|
- puts "the #{a+1} output\n"
- end
- end
- thread1.join
- thread2.join
这样两个线程都开启以后,等调用join方法等待两个线程完成。
输出的结果:
- hello thread
- 2012-07-28 19:22:15 +0800
- 1
- the 1 output
- 2
- the 2 output
- 3
- the 3 output
- 4
- the 4 output
- 5
- 6
- the 5 output
- 7
- the 6 output
- 8
- the 7 output
- 9
- the 8 output
- the 9 output
- the 10 output
可以看到输出的结果已经变得没有规律了。
挂起当前的线程
上面已经提到了,通过join方法可以等待指定的线程执行完毕。同时Ruby钟还有一种方法可以实现等待指定线程运行完毕,就是value方法,value方法还可以获得线程的值。
通过Thread.current方法可以获得线程的id。如下面的代码:
- #encoding:gbk
- require "thread"
- i=1
- puts "hello thread"
- puts Time.new
- #round=5
- #while i<round
- # puts "the #{i}th round"
- # i=i+1
- #end
- thread1=Thread.start 10 do |value|
- while i<value
- id=Thread.current
- puts "#{i} 当前执行的线程id:#{id}\n"
- i=i+1