java多线程之线程同步

/**
 * 多线程进行同步的几种方式:
 * 1,ExecutorService中的awaitTermination方法,实现线程池中同步
 * 2,CountDownLatch,线程计数,调用await方法阻塞直到子线程执行结束
 * 3,Thread的join方法,阻塞直到线程结束
 *
 */
public class ThreadSynchTest {
	static final CountDownLatch latch = new CountDownLatch(3);// 闭锁

	public static void main(String[] args) {
		f3();

	}
	/**
	 * 使用ExecutorService的shutdown和awaitTermination方法实现线程同步
	 * ExecutorService的awaitTermination方法是一个阻塞式的方法,阻塞直到线程池中所有任务执行结束,
	 * 可以设置超时时间待
	 * 
	 */
	static void f1(){
		ExecutorService executorService = Executors.newCachedThreadPool();
		long startTime = System.currentTimeMillis();
		executorService.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(200);
					System.err.println(" 线程4     执行结束");
				} catch (InterruptedException e) {
				}
				
			}
		});
		executorService.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(300);
					System.err.println(" 线程5     执行结束");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}
		});
		executorService.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(350);
					System.err.println(" 线程6     执行结束");
				} catch (InterruptedException e) {
				}
				
			}
		});
		executorService.shutdown();
		try {
			//等待线程池内的线程全部执行结束,超时时间为1天
			executorService.awaitTermination(1, TimeUnit.DAYS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(System.currentTimeMillis() - startTime);
	}
	
	
	/**
	 * 使用CountDownLatch线程同步
	 * CountDownLatch是一个同步辅助类
	 * 用给定的计数初始化CountDownLatch,该计数是需要同步的线程个数。
	 * 每次调用CountDown(),计数减1.主程序执行到await()方法会阻塞等待线程的执行,直到计数为0
	 */
	
	static void f2(){
		ExecutorService executorService = Executors.newCachedThreadPool();
		long startTime = System.currentTimeMillis();
		executorService.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(200);
					System.err.println(" 线程4     执行结束");
					ThreadSynchTest.latch.countDown();
				} catch (InterruptedException e) {
				}
				
			}
		});
		executorService.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(300);
					System.err.println(" 线程5     执行结束");
					ThreadSynchTest.latch.countDown();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
			}
		});
		executorService.execute(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(350);
					System.err.println(" 线程6     执行结束");
					ThreadSynchTest.latch.countDown();
				} catch (InterruptedException e) {
				}
				
			}
		});
		executorService.shutdown();
		try {
			ThreadSynchTest.latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println(System.currentTimeMillis() - startTime);
	}
	
	/**
	 * 使用join线程同步。
	 * join方法的主要作用就是同步,在A线程中调用了B线程的join()方法时,
	 * 表示只有当B线程执行完毕时,A线程才能继续执行。
	 * 如果主线程想等待子线程结束后进行一些操作操作,可以调用子线程的join方法,阻塞等待子线程结束。以
	 */
	static void f3(){
		long startTime = System.currentTimeMillis();
		Thread t1=new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1500);
					System.err.println(" 线程t1    执行结束");
				} catch (InterruptedException e) {
				}
			}
		});
		
		Thread t2=new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(350);
					System.err.println(" 线程t2    执行结束");
				} catch (InterruptedException e) {
				}
			}
		});
		t1.start();
		t2.start();
		try {
			t1.join();
			t2.join();
			System.out.println("两个子线程已经执行结束,进入主线程");
			System.out.println(System.currentTimeMillis() - startTime);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(java多线程之线程同步)