Java多线程系列(二十三)如何使多个线程同时等一个线程执行

1.使用countDownLatch

Thread thread1 = new Thread(new Runnable() {
					
					@Override
					public void run() {
						System.out.println("进入thread1");
						try {
							latch.await();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println("退出thread1");	
					}
				});
				Thread thread2 = new Thread(new Runnable() {
					
					@Override
					public void run() {
						System.out.println("进入thread2");
						try {
							latch.await();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println("退出thread2");	
						
					}
				});
				Thread thread3 = new Thread(new Runnable() {
					
					@Override
					public void run() {
						System.out.println("进入thread3");
						try {
							latch.await();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println("退出thread3");	
						
					}
				});
				
				
				Thread thread4 = new Thread(new Runnable() {
						
						@Override
						public void run() {
							System.out.println("进入thread4");
							System.out.println("thread4 开始sleep");
							try {
								Thread.sleep(5000);
							} catch (InterruptedException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							System.out.println("thread sleep  完成");
							try {
								latch.countDown();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							System.out.println("退出thread4");	
							
						}
					});
				
				thread1.start();
				thread2.start();
				thread3.start();
				thread4.start();

2.使用信号量

思路:先把所有的信号量申请完,然后条件满足后全部释放

代码:

public class Main {
	static Semaphore semaphore = new Semaphore(3);
	public static void main(String args[]) {
		
		try {
			semaphore.acquire();
			semaphore.acquire();
			semaphore.acquire();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		
		
				Thread thread1 = new Thread(new Runnable() {
					
					@Override
					public void run() {
						System.out.println("进入thread1");
						try {
							semaphore.acquire();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println("退出thread1");	
					}
				});
				Thread thread2 = new Thread(new Runnable() {
					
					@Override
					public void run() {
						System.out.println("进入thread2");
						try {
							semaphore.acquire();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println("退出thread2");	
						
					}
				});
				Thread thread3 = new Thread(new Runnable() {
					
					@Override
					public void run() {
						System.out.println("进入thread3");
						try {
							semaphore.acquire();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println("退出thread3");	
						
					}
				});
				Thread thread4 = new Thread(new Runnable() {
				
				@Override
				public void run() {
					System.out.println("进入thread4");
					System.out.println("thread4 开始sleep");
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.out.println("thread sleep  完成");
					try {
						semaphore.release();
						semaphore.release();
						semaphore.release();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println("退出thread4");	
					
				}
			});
				
			
				
				thread1.start();
				thread2.start();
				thread3.start();
				thread4.start();

				
		
		
		
		
		
	}

}

3.使用读写锁

主线程先获取写锁,然后所有子线程获取读锁,然后等事件发生时主线程释放写锁。

 

哈哈,这个方式比较讨巧。

你可能感兴趣的:(安卓)