日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Ruby線程相關知識點分析

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

創(chuàng)新互聯(lián)建站是一家專業(yè)提供岱岳企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站制作、做網(wǎng)站、H5技術、小程序制作等業(yè)務。10年已為岱岳眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。

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

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代碼示例一樣,父子進程共享同一段代碼?!禤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去想。結果過了十分鐘后靈光一現(xiàn)才知道是怎么回事。同志們,看英文的東西不容易??!

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

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


文章標題:Ruby線程相關知識點分析
標題路徑:http://m.5511xx.com/article/djhicpi.html