初次接触ruby线程
来源:互联网 发布:推荐一部电影 知乎 编辑:程序博客网 时间:2024/06/05 09:54
第一次写一个ruby多线程程序。但是最初有点小问题
源码:
i=1puts "hello thread"puts Time.new#round=5#while i<round#puts "the #{i}th round"#i=i+1#endthread1=Thread.start 10 do |value| while i<valueputs "#{i}"i=i+1endendthread2=Thread.start do 10.times do |a|puts "the #{a+1} output"endend
然后运行程序没有线程的运行输出。刚开始以为成为写错了,后来发现是主线程执行完毕,开启的线程还没有来得及显示数据就被关闭掉了。所以这个时候得不到任何关于线程的输出。
想要得到正确的输出,必须让线程有足够的时间来运行输出。可以加上.join来等待线程完成。
i=1puts "hello thread"puts Time.new#round=5#while i<round#puts "the #{i}th round"#i=i+1#endthread1=Thread.start 10 do |value| while i<valueputs "#{i}"i=i+1endendthread1.jointhread2=Thread.start do 10.times do |a|puts "the #{a+1} output"endendthread2.join
这个时候就可以得到线程的输出。
hello thread2012-07-28 12:51:12 +0800123456789the 1 outputthe 2 outputthe 3 outputthe 4 outputthe 5 outputthe 6 outputthe 7 outputthe 8 outputthe 9 outputthe 10 output
- 初次接触ruby线程
- Ruby on Rails 入门之:(17) 初次接触ruby线程
- 【Ruby】与ruby的初次接触(1)
- 初次接触
- 初次接触
- 初次接触
- 初次接触EJB
- 初次接触MFC
- 《英雄无敌 V》初次接触!
- 初次接触linux 总结:
- 初次接触power architecture
- 初次接触Hibernate
- 初次接触Hibernate
- 初次接触GDIplus
- 初次接触ASP.NET
- 初次接触asp.net
- WAP初次接触
- 初次接触XSL
- ffmpeg的做的很好的api参考查询网址
- Android 动画框架原理
- POJ 2062 完全背包
- 男人与女人的最爱,你是不是这样的
- POJ 2559 - Largest Rectangle in a Histogram
- 初次接触ruby线程
- PMBOK读书笔记
- HDU-2063(简单二分匹配)
- 日记2012-7-28
- paip.提升开发效率--使用拼音
- 对IP广播的简单实验
- CF Beta Round #89 (Div. 2) - Caesar's Legions
- UItableViewCell的复用机制,原理解答
- 嵌入式编译器常用输出格式--Intel 32 bit Hex