// the thread.sleep that follows t.start() to see the difference.
t.start();
//thread.sleep(0);
for (int i = 0; i < 4; i++) {
console.writeline("main thread: do some work.");
thread.sleep(0);
}
console.writeline("main thread: call join(), to wait until threadproc ends.");
t.join();
console.writeline("main thread: threadproc.join has returned. press enter to end program.");
console.readline();
}
}
此代码产生的输出类似如下内容:
main thread: start a second thread. main thread: do some work. threadproc: 0 main thread: do some work. threadproc: 1 main thread: do some work. threadproc: 2 main thread: do some work. threadproc: 3 main thread: call join(), to wait until threadproc ends. threadproc: 4 threadproc: 5 threadproc: 6 threadproc: 7 threadproc: 8 threadproc: 9 main thread: threadproc.join has returned. press enter to end program.