让子线程中断主线程工作的2种办法

1.Thread.join

代码如下:

  1. package demo;
  2. import java.util.Random;
  3. public class WaitAllSubThread {      
  4.     
  5. //    int liveThreadNum;//记录运行的子线程数    
  6.     
  7.     int n;  //工作线程数      
  8.      
  9.     public WaitAllSubThread(int n) {      
  10.         this.n = n;      
  11.     }      
  12.      
  13.     class Worker implements Runnable {      
  14.      
  15.         String name;      
  16.         int sleep;      
  17.      
  18.         public Worker(String name, int sleep) {      
  19.             this.name = name;      
  20.             this.sleep = sleep;      
  21.         }      
  22.      
  23.         public void run() {      
  24.             
  25. //          upLive(); //计算此线程已经工作.   
  26.             System.out.println(name+", start to work.");      
  27.             try {      
  28.                 Thread.sleep(sleep);    //虚拟工作. 10s 随机时间      
  29.             } catch (InterruptedException e) {      
  30.                 System.out.println(name+" interrupted.");      
  31.             }      
  32.             System.out.println(name+", end to work ["+sleep+"] sleep.");      
  33.             
  34. //          downLive();   //此线程工作完成         
  35.         }      
  36.     }
  37.     
  38. //    //记录线程数的同步方法.    
  39. //    private synchronized void downLive() {    
  40. //        liveThreadNum--;    
  41. //    }    
  42. //   
  43. //    private synchronized void upLive() {    
  44. //        liveThreadNum++;    
  45. //    }    
  46. //   
  47. //    private synchronized boolean isLive() {    
  48. //        return liveThreadNum > 0;    
  49. //    }   
  50.      
  51.     public void run() {  
  52.         
  53.         System.out.println("-------------main run start-------------");      
  54.         int sleepSaid = 10 * 1000;  //每个工作线程虚拟工作最大时间      
  55.         Random rm = new Random();
  56.         
  57.         Thread[] ths = new Thread[n];
  58.         for(int i=0; i< ths.length; i++) {      
  59.             ths[i] = new Thread(new Worker("worker-"+i, rm.nextInt(sleepSaid)+1));
  60.             ths[i].start();      
  61.         }      
  62.      
  63.         for(Thread th : ths) {      
  64.             try {      
  65.                 th.join();//join方式      
  66.             } catch (InterruptedException e) {  
  67.                 e.printStackTrace();      
  68.             }      
  69.         }      
  70. //        //等待所有工作线程完成.    
  71. //        while(isLive()) {    
  72. //            try {    
  73. //                Thread.sleep(1000); //每隔1s查看下是否所有线程完成.
  74. //            } catch (InterruptedException e) {    
  75. //                System.out.println("main thread sleep interrupted.");    
  76. //            }    
  77. //        }   
  78.         System.out.println("---------------main run end--------------");      
  79.     }      
  80.      
  81.     public static void main(String[] args) {      
  82.         WaitAllSubThread wast = new WaitAllSubThread(10);      
  83.         wast.run();      
  84.     }      
  85. }

2.CountDownLatch

代码如下:

  1. package demo;
  2. import java.util.Random;
  3. import java.util.concurrent.CountDownLatch;
  4. public class CountDownLatchUse {
  5.     
  6.     final CountDownLatch downLatch;      
  7.     int n;  //工作线程数      
  8.      
  9.     public CountDownLatchUse(int n) {      
  10.         this.downLatch = new CountDownLatch(n);      
  11.         this.n = n;      
  12.     }      
  13.      
  14.     class Worker implements Runnable {      
  15.      
  16.         String name;      
  17.         int sleep;      
  18.      
  19.         public Worker(String name, int sleep) {      
  20.             this.name = name;      
  21.             this.sleep = sleep;      
  22.         }      
  23.      
  24.         public void run() {      
  25.             System.out.println(name+", start to work.");      
  26.             try {      
  27.                 Thread.sleep(sleep);    //虚拟工作. 10s 随机时间      
  28.             } catch (InterruptedException e) {      
  29.                 System.out.println(name+" interrupted.");      
  30.             }      
  31.             System.out.println(name+", end to work ["+sleep+"] sleep.");      
  32.             meDone();   //某个工作线程完成      
  33.         }      
  34.     }      
  35.      
  36.     private void meDone() {      
  37.         downLatch.countDown();      
  38.     }      
  39.      
  40.     public void run() { 
  41.         
  42.         System.out.println("-------------main run start-------------");      
  43.         int sleepSaid = 10 * 1000;  //每个工作线程虚拟工作最大时间      
  44.         Random rm = new Random();      
  45.         for(int i=0; i<n; i++) {      
  46.             new Thread(new Worker("worker-"+i, rm.nextInt(sleepSaid)+1)).start();      
  47.         }      
  48.      
  49.         try {      
  50.             downLatch.await();  //等待所有工作线程完成.      
  51.         } catch (InterruptedException e) {      
  52.             System.out.println("main interrupted.");      
  53.         }      
  54.         System.out.println("---------------main run end--------------");      
  55.     }      
  56.      
  57.     public static void main(String[] args) {     
  58.         
  59.         CountDownLatchUse mtu = new CountDownLatchUse(10);      
  60.         mtu.run();
  61.     }      
  62. }

你可能感兴趣的:(thread,工作,String,ini)