在上文Lua coroutine 不一样的多线程编程思路中想到coroutine的运行机制跟Java中的wait/notify很相似,所以写了一个简单程序比较一下。
源代码
Lua code
co = coroutine.create(function(loops)
for i = 1, loops do
coroutine.yield()
end
end)
local x = os.clock()
local loops = 100 * 1000 * 1000
coroutine.resume(co, loops)
for i = 1, loops do
coroutine.resume(co)
end
print(string.format("elapsed time: %.2f\n", os.clock() - x))
Java code
public class TestWait {
public static void main(String[] args) {
WaitClass wc = new WaitClass();
wc.start();
int loops = 100 * 1000 * 1000;
long t1 = System.currentTimeMillis();
for (int i = 0; i < loops; i++) {
synchronized (wc) {
wc.notify();
}
}
l