Comment detail

ワーカスレッドを安全に終了させるまで待機 (Nested Flatten)

This comment is reply for 4965 sumim: Squeak Smalltalk のスレ...(ワーカスレッドを安全に終了させるまで待機). Go to thread root.

GNU Smalltalk に訳してみました。シンプルなのがいいですね。

1
2
3
4
5
6
7
8
9
| out | out := SharedQueue new.
((Smalltalk arguments at: 1 ifAbsent: [ ^'usage: gst 116.st -a <nThread>' displayNl ])
 asNumber timesRepeat: [
  [| secs |
    out nextPut: { #start. secs := Random next * 11 // 1 + 5 }.
    (Delay forSeconds: secs) wait.
    out nextPut: { #stop.  secs }
  ] fork
]) * 2 timesRepeat: [ out next printNl ]!

さらに Rhino で。

1
2
3
4
5
6
7
8
9
(function doukaku116(nThread){
  var out = new java.util.concurrent.ArrayBlockingQueue(nThread * 2);
  for(var i = nThread; i--;) spawn(function(secs){
    out.put('#start '+ (secs = Math.random() * 11 + 5 | 0));
    java.lang.Thread.sleep(secs * 1000);
    out.put('#stop  '+ secs);
  });
  for(i = nThread * 2; i--;) print(out.take());
})(5);

Index

Feed

Other

Link

Pathtraq

loading...