Saturday, December 02, 2006

Pages 132-3


Listen to this article.

Value: Medium

Level: Average

Summary:
You can control thread scheduler. Invoking Thread.stop stops the current thread, and invoking Thread#run. Thread.pass deschedules the current thread, allowing others to run, and Thread#join and Thread#value suspend the calling thread until a given thread finishes.

Memo: If possible, leave threads independent.

Example:
class Chaserattr_reader :countdef initialize(name)@name = name@count = 0enddef chase(other)while @count <> 1Thread.pass #endprint "#@name: #{count}\n"@count += 1 print "#@name: #{count}\n"endendendc1 = Chaser.new("A")c2 = Chaser.new("B")threads = [Thread.new { Thread.stop; c1.chase(c2) },Thread.new { Thread.stop; c2.chase(c1) }]start_index = rand(2)threads[start_index].runthreads[1 - start_index].runthreads.each {t t.join }

Reported errata (at 10/17/06 14:17:18 PDT): 0

Errata I found: 0

My suggestions to the author: 0Doubts: 0

0 Comments:

Post a Comment

<< Home