我需要阻止一个进程运行超过 n 秒,所以这就是我想我会做的:
|aProcess|
aProcess := [ 10000 timesRepeat: [Transcript show: 'X'] ] fork.
[(Delay forSeconds: 1) wait. aProcess terminate] fork.
我认为这是继续进行的正确方法,但似乎有时会失败,成绩单只是继续打印 Xes。让我烦恼的是它有时确实有效,但我无法弄清楚工作/失败模式是什么。
两个进程都以相同的优先级运行,这就是为什么第二个进程实际上根本没有机会中断第一个进程。尝试以较低或更好的优先级运行第一个循环,以更高的优先级运行第二个循环:
[(Delay forSeconds: 1) wait. aProcess terminate]
forkAt: Processor userInterruptPriority
这已经在图书馆里了,你不需要重新发明它。
[10000 timesRepeat: [Transcript show: 'X']]
valueWithin: 1 second onTimeout: [Transcript show: 'stop']