1. Demo1 - thread.join()
public class MyTask1 implements Runnable { private int num ; public MyTask1(int num){ this.num = num; } /* * @see java.lang.Runnable#run() */ @Override public void run() { for (int i = 0; i < 2; i++) { System.out.println(num + "----" +i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Thread" + num + " End."); } }
import java.util.ArrayList; import java.util.List; public class TestThreadMain1 { /** * @Title main * @Description TODO * @Author weizhi2018 * @param args * @throws */ public static void main(String[] args) { long startTime = System.currentTimeMillis(); List<Thread> list = new ArrayList<Thread>(); int num = 5; for(int i=0;i<num;i++){ Thread thread = new Thread(new MyTask1(i)); thread.start(); list.add(thread); } for(Thread thread:list){ try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } long endTime = System.currentTimeMillis(); System.out.println("RunTime:"+ (endTime - startTime)); } }output:
0----0 4----0 2----0 3----0 1----0 1----1 2----1 3----1 4----1 0----1 Thread2 End. Thread4 End. Thread0 End. Thread1 End. Thread3 End. RunTime:20172. Demo2 - countDownLatch.await()
import java.util.concurrent.CountDownLatch; public class MyTask2 implements Runnable { private CountDownLatch countDownLatch ; public MyTask2(CountDownLatch countDownLatch){ this.countDownLatch = countDownLatch; } /* * @see java.lang.Runnable#run() */ @Override public void run() { for (int i = 0; i < 2; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " End."); countDownLatch.countDown(); } }
import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class TestThreadMain2 { /** * @Title main * @Description TODO * @Author weizhi2018 * @param args * @throws */ public static void main(String[] args) { long startTime = System.currentTimeMillis(); int num = 5; CountDownLatch countDownLatch = new CountDownLatch(num); for(int i=0;i<num;i++){ Thread thread = new Thread(new MyTask2(countDownLatch)); thread.start(); } try { //等待,直到计数器为0 countDownLatch.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); System.out.println("RunTime:"+ (endTime - startTime)); } }output:
Thread-4 End. Thread-0 End. Thread-2 End. Thread-3 End. Thread-1 End. RunTime:20163.主线程等待线程池执行完再执行
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class TestThreadPoolMain { /** * @Title main * @Description TODO * @Author weizhi2018 * @param args * @throws */ public static void main(String[] args) { long start = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(2); for(int i = 0; i < 3; i++) { Thread thread = new Thread(new MyTask1(i)); executor.execute(thread); } executor.shutdown(); try { // awaitTermination返回false即超时会继续循环,返回true即线程池中的线程执行完成主线程跳出循环往下执行,每隔10秒循环一次 while (!executor.awaitTermination(10, TimeUnit.SECONDS)); } catch (InterruptedException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("RunTime:" + (end - start)); } }output:
0----0 1----0 0----1 1----1 Thread1 End. 2----0 Thread0 End. 2----1 Thread2 End. RunTime:4019