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线程的相关概念详解,希望对大家有所帮助。

责任编辑:曹凯 来源: 博客园
相关推荐

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

大数据数据库信息安全

2020-10-07 15:15:41

Python

2010-09-02 10:11:11

华为认证

2010-08-18 10:52:46

Linux笔试

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

2009-12-15 10:31:30

Ruby rails页

2023-09-08 13:46:12

ArrayList数据存储容器

2021-04-13 08:25:12

测试开发Java注解Spring

2017-11-01 15:38:54

jvm知识点总览

2010-12-29 10:14:09

2020-07-01 17:25:28

Redis数据库内存
点赞
收藏

51CTO技术栈公众号