偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Ruby線程相關(guān)知識點分析

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

Ruby語言一款完全面向?qū)ο蟮慕忉屝湍_本語言。對于這樣的一款新型編程語言,其特性對于程序員的吸引力不小。我們先來了解一下Ruby線程的相關(guān)概念。#t#

今天看了Ruby線程部分?!禤rogramming Ruby》***版的HTML版的線程和進(jìn)程部分講得很詳細(xì)。看完后感覺就好像又把操作系統(tǒng)的這一部分重溫了一遍。尤其是Spawning New Processes那一節(jié),如果沒有學(xué)過操作系統(tǒng)還真不知道他說什么。

IO.popen,其中的popen,我理解應(yīng)該是應(yīng)該是"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代碼示例一樣,父子進(jìn)程共享同一段代碼?!禤rogramming 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. ”。

***次看我完全沒看出來他說的是什么??戳舜a后一時間也沒往fork去想。結(jié)果過了十分鐘后靈光一現(xiàn)才知道是怎么回事。同志們,看英文的東西不容易??!

Ruby線程還挺好學(xué)。Ruby線程的功能是自已實現(xiàn)的。與操作系統(tǒng)無關(guān)。為了達(dá)到平臺無關(guān)性,這種犧牲我覺得有點大。不說作者開發(fā)時得費多少力氣。就是使用起來,也沒有本地線程的種種優(yōu)勢。比如說線程饑餓。下面我寫了一個練習(xí)性質(zhì)的生產(chǎn)者--消費者例子。實話說,比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 } 

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

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2025-07-09 09:05:00

2025-05-07 08:55:00

2022-08-16 15:17:37

機(jī)器學(xué)習(xí)算法模型

2020-12-24 13:32:31

大數(shù)據(jù)數(shù)據(jù)分析SQL

2010-08-17 14:56:00

HCNE認(rèn)證

2011-04-15 12:25:21

BGP路由

2016-05-30 17:31:34

Spring框架

2019-07-10 15:46:05

大數(shù)據(jù)數(shù)據(jù)庫信息安全

2009-12-30 17:23:49

Silverlight

2025-05-19 10:00:00

MySQL數(shù)據(jù)庫InnoDB

2010-08-18 10:52:46

Linux筆試

2010-09-02 10:11:11

華為認(rèn)證

2020-10-07 15:15:41

Python

2010-07-27 15:49:28

Flex

2009-08-06 17:42:32

C#知識點

2010-06-17 16:42:04

UML

2021-01-18 10:33:53

Java反射模塊

2009-12-15 10:31:30

Ruby rails頁

2023-09-08 13:46:12

ArrayList數(shù)據(jù)存儲容器

2021-04-13 08:25:12

測試開發(fā)Java注解Spring
點贊
收藏

51CTO技術(shù)棧公眾號