成人免费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

2022-08-16 15:17:37

機器學習算法模型

2020-12-24 13:32:31

大數據數據分析SQL

2010-08-17 14:56:00

HCNE認證

2011-04-15 12:25:21

BGP路由

2016-05-30 17:31:34

Spring框架

2009-12-30 17:23:49

Silverlight

2019-07-10 15:46:05

大數據數據庫信息安全

2010-08-18 10:52:46

Linux筆試

2010-09-02 10:11:11

華為認證

2020-10-07 15:15:41

Python

2025-05-19 10:00:00

MySQL數據庫InnoDB

2021-01-18 10:33:53

Java反射模塊

2010-07-27 15:49:28

Flex

2009-08-06 17:42:32

C#知識點

2010-06-17 16:42:04

UML

2021-04-13 08:25:12

測試開發Java注解Spring

2011-04-01 15:28:40

Zabbix配置安裝

2020-07-01 17:25:28

Redis數據庫內存

2017-11-01 15:38:54

jvm知識點總覽
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 人碰人操 | 欧美精品一区三区 | 日韩精品免费一区二区在线观看 | 国产精品久久久久久久 | 亚洲激情第一页 | 五月花丁香婷婷 | 国产精品久久久久久238 | 久久国产精品免费一区二区三区 | 九九热在线视频免费观看 | 欧美三区在线观看 | 久久久精品国产 | 有码在线 | 日韩欧美在线一区 | 美女视频一区 | 日韩在线免费 | 久久精品小视频 | 亚洲人成免费 | 成人在线视频网 | 久久久毛片| 免费观看黄色一级片 | 一本大道久久a久久精二百 国产成人免费在线 | 午夜天堂精品久久久久 | 涩涩鲁亚洲精品一区二区 | 欧美www在线 | 精品久久久久久久久久久 | 日本精品久久久久 | 精品国产18久久久久久二百 | 欧美性极品xxxx做受 | 成人在线一区二区 | 毛片国产 | 武道仙尊动漫在线观看 | 国产在线97 | 中文字幕视频在线 | 国产精品久久 | 久久久成人免费一区二区 | 91国内精精品久久久久久婷婷 | 国产高清一二三区 | 国产伦一区二区三区视频 | 成人欧美一区二区三区在线观看 | 久久精品国产一区二区电影 | 综合一区二区三区 |