join()
方法使所有的调用线程等待直到当前线程(已连接线程)终止或完成其任务。
参考以下示例 -
using system;
using system.threading;
public class mythread
{
public void thread1()
{
for (int i = 0; i < 5; i++)
{
console.writeline("thread1 : "+i);
thread.sleep(500);
}
}
}
public class threadexample
{
public static void main()
{
mythread mt = new mythread();
thread t1 = new thread(new threadstart(mt.thread1));
thread t2 = new thread(new threadstart(mt.thread1));
thread t3 = new thread(new threadstart(mt.thread1));
t1.start();
t1.join();
t2.start();
t3.start();
}
}
执行上面示例代码,得到以下结果 -
thread1 : 0
thread1 : 1
thread1 : 2
thread1 : 3
thread1 : 4
thread1 : 0
thread1 : 0
thread1 : 1
thread1 : 1
thread1 : 2
thread1 : 2
thread1 : 3
thread1 : 3
thread1 : 4
thread1 : 4