溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行Ruby線程相關(guān)知識(shí)點(diǎn)分析

發(fā)布時(shí)間:2022-01-11 14:44:09 來(lái)源:億速云 閱讀:108 作者:柒染 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何進(jìn)行Ruby線程相關(guān)知識(shí)點(diǎn)分析,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

Ruby語(yǔ)言一款完全面向?qū)ο蟮慕忉屝湍_本語(yǔ)言。對(duì)于這樣的一款新型編程語(yǔ)言,其特性對(duì)于程序員的吸引力不小。

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

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 

簡(jiǎn)直和Unix課里面的fork代碼示例一樣,父子進(jìn)程共享同一段代碼?!禤rogramming Ruby》對(duì)這段代碼的解釋是“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. ”。

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

Ruby線程還挺好學(xué)。Ruby線程的功能是自已實(shí)現(xiàn)的。與操作系統(tǒng)無(wú)關(guān)。為了達(dá)到平臺(tái)無(wú)關(guān)性,這種犧牲我覺得有點(diǎn)大。不說(shuō)作者開發(fā)時(shí)得費(fèi)多少力氣。就是使用起來(lái),也沒有本地線程的種種優(yōu)勢(shì)。比如說(shuō)線程饑餓。下面我寫了一個(gè)練習(xí)性質(zhì)的生產(chǎn)者--消費(fèi)者例子。實(shí)話說(shuō),比Ruby中thread.rb里的例子要長(zhǎng)太多……好處是,這里解決了屏幕輸出時(shí)的竄行問題。

  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 } 

上述就是小編為大家分享的如何進(jìn)行Ruby線程相關(guān)知識(shí)點(diǎn)分析了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI