现在的位置: 首页 > 综合 > 正文

Ruby on Rails 入门之:(18) ruby线程控制,线程同步

2013年10月14日 ⁄ 综合 ⁄ 共 2231字 ⁄ 字号 评论关闭

在Ruby中线程是用户级线程并依赖与操作系统。线程是进程中的一个实体,是被系统独立调度和分配的基本单位。线程可以与同属于同样个进程的其他线程共同共享进程的全部资源。但是线程不拥有资源,只需要一点在运行时必不可少的资源。

线程的基本控制,如果主进程结束,会结束所有的线程。所以如果你的线程在执行显示的任务,但是主进程却已经执行完毕,会看不到线程的任何输出。如链接中所示:

http://blog.csdn.net/weixingstudio/article/details/7796442

线程的创建可以使用Thread.new,同样可以以同样的语法使用Thread.start 或者Thread.fork这三个方法来创建线程。

创建线程并执行的基本代码:

[ruby] view
plain
copy

  1. i=1  
  2. puts "hello thread"  
  3. puts Time.new  
  4.   
  5. #round=5  
  6. #while i<round  
  7. #   puts "the #{i}th round"  
  8. #   i=i+1  
  9. #end  
  10.   
  11. thread1=Thread.start 10 do |value|  
  12.     while i<value  
  13.         puts "#{i}"  
  14.         i=i+1  
  15.     end  
  16. end  
  17. thread1.join  
  18.   
  19. thread2=Thread.start do   
  20.     10.times do |a|  
  21.         puts "the #{a+1} output"  
  22.     end  
  23. end  
  24. thread2.join  


上面的代码中,线程thread1线程创建以后,调用了thread1.join方法,这个方法会挂起主线程,等待thread1线程完成,这样就可一在主线程结束之前看到线程thread1,thread2的输出消息。

上面的线程的运行结果:

[ruby] view
plain
copy

  1. 1  
  2. 2  
  3. 3  
  4. 4  
  5. 5  
  6. 6  
  7. 7  
  8. 8  
  9. 9  
  10. the 1 output  
  11. the 2 output  
  12. the 3 output  
  13. the 4 output  
  14. the 5 output  
  15. the 6 output  
  16. the 7 output  
  17. the 8 output  
  18. the 9 output  
  19. the 10 output  


但是这样可以看到,线程的输出是等到线程thread1完全执行完毕以后才执行线程thread2,有经验的朋友就会知道线程的调度是随机的,所以如果没有thread1.join和thread2.join两个方法,线程的输出就会变得没有规律,下面我们该一下程序看看没有规律的线程输出。

[ruby] view
plain
copy

  1. i=1  
  2. puts "hello thread"  
  3. puts Time.new  
  4.   
  5. #round=5  
  6. #while i<round  
  7. #   puts "the #{i}th round"  
  8. #   i=i+1  
  9. #end  
  10.   
  11. thread1=Thread.start 10 do |value|  
  12.     while i<value  
  13.         puts "#{i}\n"  
  14.         i=i+1  
  15.     end  
  16. end  
  17.   
  18.   
  19. thread2=Thread.start do   
  20.     10.times do |a|  
  21.         puts "the #{a+1} output\n"  
  22.     end  
  23. end  
  24.   
  25. thread1.join  
  26. thread2.join  


这样两个线程都开启以后,等调用join方法等待两个线程完成。

输出的结果:

[ruby] view
plain
copy

  1. hello thread  
  2. 2012-07-28 19:22:15 +0800  
  3. 1  
  4. the 1 output  
  5. 2  
  6. the 2 output  
  7. 3  
  8. the 3 output  
  9. 4  
  10. the 4 output  
  11. 5  
  12. 6  
  13. the 5 output  
  14. 7  
  15. the 6 output  
  16. 8  
  17. the 7 output  
  18. 9  
  19. the 8 output  
  20. the 9 output  
  21. the 10 output  


可以看到输出的结果已经变得没有规律了。

挂起当前的线程

上面已经提到了,通过join方法可以等待指定的线程执行完毕。同时Ruby钟还有一种方法可以实现等待指定线程运行完毕,就是value方法,value方法还可以获得线程的值。
通过Thread.current方法可以获得线程的id。如下面的代码:
[ruby] view
plain
copy

  1. #encoding:gbk  
  2. require "thread"   
  3.   
  4.   
  5. i=1  
  6. puts "hello thread"  
  7. puts Time.new  
  8.   
  9. #round=5  
  10. #while i<round  
  11. #   puts "the #{i}th round"  
  12. #   i=i+1  
  13. #end  
  14.   
  15. thread1=Thread.start 10 do |value|  
  16.     while i<value  
  17.         id=Thread.current  
  18.         puts "#{i} 当前执行的线程id:#{id}\n"  
  19.         i=i+1  
  20.     

抱歉!评论已关闭.