主線程等待子線程結束后再運行


1.join

將子線程插入到主線程中,主線程和子線程合並為順序執行的線程

 1 public class Test {
 2     public static void main(String[] args) throws Exception {
 3         List<Thread> list = new ArrayList<Thread>();
 4         for(int i=0; i<10; i++){
 5             Thread thread = new Thread(new Runnable(){
 6                 public void run(){
 7                     try {
 8                         Thread.sleep(5*1000);
 9                     } catch (Exception e) {
10                         // TODO: handle exception
11                     }
12                     System.out.println("子線程執行完畢!");
13                 }
14             });
15             list.add(thread);
16             thread.start();
17         }
18         for(Thread thread : list){
19             thread.join();
20         }
21         System.out.println("主線程執行完畢!");
22     }
23 }

 

2.CountDownLatch

 1 public class Test {
 2     public static void main(String[] args) throws Exception {
 3         List<Thread> list = new ArrayList<Thread>();
 4         final CountDownLatch latch = new CountDownLatch(10);
 5         for(int i=0; i<10; i++){
 6             Thread thread = new Thread(new Runnable(){
 7                 public void run(){
 8                     try {
 9                         Thread.sleep(5*1000);
10                     } catch (Exception e) {
11                         // TODO: handle exception
12                     }
13                     latch.countDown();
14                     System.out.println("子線程執行完畢!");
15                 }
16             });
17             list.add(thread);
18             thread.start();
19         }
20         
21         latch.await();
22         System.out.println("主線程執行完畢!");
23     }
24 }

 

3.CyclicBarrier

 1 public class Test {
 2     public static void main(String[] args) throws Exception {
 3         List<Thread> list = new ArrayList<Thread>();
 4         final CyclicBarrier cyclic = new CyclicBarrier(10);
 5         for(int i=0; i<10; i++){
 6             new Thread(new Runnable(){
 7                 public void run(){
 8                     try {
 9                         Thread.sleep(5*1000);
10                     } catch (Exception e) {
11                         // TODO: handle exception
12                     }
13                     try {
14                         cyclic.await();
15                     } catch (InterruptedException e) {
16                         // TODO Auto-generated catch block
17                         e.printStackTrace();
18                     } catch (BrokenBarrierException e) {
19                         // TODO Auto-generated catch block
20                         e.printStackTrace();
21                     }
22                     System.out.println("子線程執行完畢!");
23                 }
24             }).start();;
25         }
26         
27         cyclic.await();
28         System.out.println("主線程執行完畢!");
29     }
30 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM