jiongliu 2010-09-17 11:42
浏览 212
已采纳

联合线程 join()问题

class ThreadDemo1
{
public static void main(String []args)
{
Thread tt=new TestThread();
tt.start();
int index =0;
while(true)
{
if(index++ == 100)
try
{
tt.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println("main"+Thread.currentThread().getName());
}
}
}
class TestThread extends Thread
{
public void run()
{
while(true)
{
System.out.println("run"+Thread.currentThread().getName());
}
}
}

//上面的程序是让两个 线程交替执行 然后再将子线程合并到主线程中去的 但是打印出的结果是这样的
runThread-0 一直循环
为什么会只有一个线程呢 应该有两个的
请朋友帮忙看看

  • 写回答

1条回答 默认 最新

  • uncontrolsheep 2010-09-18 07:54
    关注

    我觉得是不是因为index太小导致一个线程还没执行就join变成一个了?试试加大index的值或者sleep一下?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?