成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Ruby線程相關知識點分析

開發 后端
Ruby線程其實對于初學者來說還是比較容易學的。通過一段代碼示例,大致就可以了解到Ruby線程的相關概念,只有實踐才能真正掌握所學到的知識。

Ruby語言一款完全面向對象的解釋型腳本語言。對于這樣的一款新型編程語言,其特性對于程序員的吸引力不小。我們先來了解一下Ruby線程的相關概念。#t#

今天看了Ruby線程部分。《Programming Ruby》***版的HTML版的線程和進程部分講得很詳細。看完后感覺就好像又把操作系統的這一部分重溫了一遍。尤其是Spawning New Processes那一節,如果沒有學過操作系統還真不知道他說什么。

IO.popen,其中的popen,我理解應該是應該是"piped open"的意思。其中這段Ruby線程代碼,

  1. pipe = IO.popen("-","w+")  
  2. if pipe  
  3. pipe.puts "Get a job!"  
  4. $stderr.puts "Child says
     '#{pipe.gets.chomp}'"  
  5. else  
  6. $stderr.puts "Dad says 
    '#{gets.chomp}'"  
  7. puts "OK"  
  8. end 

簡直和Unix課里面的fork代碼示例一樣,父子進程共享同一段代碼。《Programming Ruby》對這段代碼的解釋是“There's one more twist to popen. If the command you pass it is a single minus sign (``--''), popen will fork a new Ruby interpreter. Both this and the original interpreter will continue running by returning from the popen. The original process will receive an IO object back, while the child will receive nil. ”。

***次看我完全沒看出來他說的是什么。看了代碼后一時間也沒往fork去想。結果過了十分鐘后靈光一現才知道是怎么回事。同志們,看英文的東西不容易啊!

Ruby線程還挺好學。Ruby線程的功能是自已實現的。與操作系統無關。為了達到平臺無關性,這種犧牲我覺得有點大。不說作者開發時得費多少力氣。就是使用起來,也沒有本地線程的種種優勢。比如說線程饑餓。下面我寫了一個練習性質的生產者--消費者例子。實話說,比Ruby中thread.rb里的例子要長太多……好處是,這里解決了屏幕輸出時的竄行問題。

  1. require 'thread'  
  2. class Consumer  
  3. def initialize(queue, 
    stdout_mutex)  
  4. @queuequeue = queue  
  5. @stdout_mutexstdout_mutex 
    = stdout_mutex  
  6. end  
  7. def consume  
  8. product = @queue.pop  
  9. @stdout_mutex.synchronize {  
  10. puts "Product #{product} 
    consumed."  
  11. $stdout.flush  
  12. }  
  13. end  
  14. end  
  15. class Producer  
  16. def initialize(queue, stdout_mutex)  
  17. @queuequeue = queue  
  18. end  
  19. def produce  
  20. product = rand(10)  
  21. @queue.push(product)  
  22. @stdout_mutex.synchronize {  
  23. puts "Product #{product} produced."  
  24. $stdout.flush  
  25. }  
  26. end  
  27. end  
  28. sized_queue = SizedQueue.new(10)  
  29. stdout_mutex = Mutex.new  
  30. consumer_threads = []  
  31. 100.times {  
  32. consumer_threads << Thread.new {  
  33. consumer = Consumer.new(sized_
    queue, stdout_mutex)  
  34. consumer.consume  
  35. }  
  36. Thread.new {  
  37. producer = Producer.new(sized_
    queue, stdout_mutex)  
  38. producer.produce  
  39. }  
  40. }  
  41. consumer_threads.each { 
    |thread| thread.join } 

以上就是有關Ruby線程的相關概念詳解,希望對大家有所幫助。

責任編輯:曹凱 來源: 博客園
相關推薦

2025-05-07 08:55:00

2025-07-09 09:05:00

2022-08-16 15:17:37

機器學習算法模型

2020-12-24 13:32:31

大數據數據分析SQL

2011-04-15 12:25:21

BGP路由

2010-08-17 14:56:00

HCNE認證

2016-05-30 17:31:34

Spring框架

2019-07-10 15:46:05

大數據數據庫信息安全

2009-12-30 17:23:49

Silverlight

2020-10-07 15:15:41

Python

2010-08-18 10:52:46

Linux筆試

2025-05-19 10:00:00

MySQL數據庫InnoDB

2010-09-02 10:11:11

華為認證

2010-07-27 15:49:28

Flex

2009-08-06 17:42:32

C#知識點

2021-01-18 10:33:53

Java反射模塊

2010-06-17 16:42:04

UML

2009-12-15 10:31:30

Ruby rails頁

2023-09-08 13:46:12

ArrayList數據存儲容器

2011-04-01 15:28:40

Zabbix配置安裝
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美一级欧美三级在线观看 | 日韩视频区| 日韩精品在线一区 | 成人欧美一区二区三区黑人孕妇 | 欧美电影一区 | 国产美女永久免费无遮挡 | 欧美一区二区三区在线看 | 97久久精品午夜一区二区 | 男女免费观看在线爽爽爽视频 | 国产露脸国语对白在线 | 亚洲一二三区精品 | 婷婷色国产偷v国产偷v小说 | 国产清纯白嫩初高生在线播放视频 | 亚洲欧美一区在线 | 欧美精品一区在线发布 | 国产一区91精品张津瑜 | 成人午夜毛片 | 午夜精品一区二区三区在线 | 暖暖日本在线视频 | 国产1区2区3区 | 在线国产视频观看 | 精产嫩模国品一二三区 | 国产在线观看av | 国产欧美日韩综合精品一区二区 | 日本欧美国产在线 | 狠狠色综合久久婷婷 | 日韩欧美理论片 | 麻豆视频在线免费看 | 美女久久视频 | 国产精品一区二区三区在线 | 久久久男人的天堂 | 日韩中文字幕视频在线观看 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 在线免费黄色小视频 | 天天色av | 欧美精品一区二区在线观看 | 91av在线免费看 | 久久9久| 久久久av一区 | 国产九九九九 | 午夜视频在线播放 |