2013-02-18 39 views
0

我是线程世界的一个新手段,仍然在学习,因为我正在通过线程的概念并加入其他线程等待早期线程完成的概念,并从中加入结束,请你告诉我,我想启动三个线程T1,T2,T3,其中t2将在T1完成后启动。关于按顺序连接线程

+5

如果你只是想有一个线程运行的时间,何苦在所有创建三个线程? – 2013-02-18 17:03:19

+0

我觉得还有T3。 – Vlad 2013-02-18 17:04:15

+1

你可以在这里找到一个简单的解决方案:http://stackoverflow.com/a/13695190/469220 – Vlad 2013-02-18 17:05:34

回答

2

要等到线程1是完全做得到,然后启动线程2我明白了什么,而线3可随处运行。简单的代码,我想满足你的问题:

Thread thread1 = new Thread1(); 
Thread thread2 = new Thread2(); 
Thread thread3 = new Thread3(); 
thread3.start(); 
thread1.start(); 
try { 
    thread1.join(); 
    thread2.start(); 
} catch (InterruptedException e) { 
    //if you do not use thread1.interrupt() this will not happen. 
} 
0

做这样的事情:

 Thread T1 = new Thread(new ThreadExm); // where ThreadExm implements Runnable 
     Thread T2 = new Thread(new ThreadExm); 

     try { 
      // Start the thread1 and waits for this thread to die before 
      // starting the thread2 thread. 
      T1.start(); 
      T2.join(); 

      // Start thread2 when T1 gets completed 
      thread2.start(); 
     } catch (InterruptedException ex) { 
      ex.printStackTrace(); 
     }