CountDownLatch
public static void testCountDownLatch(String[] args) { final CountDownLatch start = new CountDownLatch(1); final CountDownLatch end = new CountDownLatch(4); final Runnable runnable = new Runnable() { @Override public void run() { try { int id = new Random().nextInt(10); start.await(); System.out.println(id + " Start"); Thread.sleep(id * 1000); System.out.println(id + " end"); end.countDown(); } catch (InterruptedException e) { throw new RuntimeException(e); } } }; new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); start.countDown(); try { end.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("All End"); }
CyclicBarrier
public static void testCyclicBarrier(String[] args) { final CyclicBarrier barrier = new CyclicBarrier(4, new Runnable() { @Override public void run() { System.out.println("In Handler"); } }); final Runnable runnable = new Runnable() { @Override public void run() { int id = new Random().nextInt(10); try { for (int i = 0; i < 3; i++) { System.out.println(id + " Start"); Thread.sleep(id * 1000); System.out.println(id + " end"); barrier.await(); } } catch (InterruptedException | BrokenBarrierException e) { throw new RuntimeException(e); } } }; new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); }
Semaphore
public static void testSemaphore(String[] args) { final Semaphore semaphore = new Semaphore(3); final Runnable runnable = new Runnable() { @Override public void run() { int id = new Random().nextInt(10); try { semaphore.acquire(); System.out.println(id + " Start"); Thread.sleep(3000); System.out.println(id + " end"); } catch (InterruptedException e) { e.printStackTrace(); } finally { semaphore.release(); } } }; new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); new Thread(runnable).start(); }
SynchronousQueue
public static void testSynchronousQueue(String[] args) { final SynchronousQueue<Integer> sq = new SynchronousQueue<>(); new Thread(new Runnable() { @Override public void run() { try { for (int i = 0; i < 3; i++) { Thread.sleep(new Random().nextInt(5) * 1000); sq.put(i); System.out.println("Put " + i); } } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 3; i++) { try { int m = sq.take(); System.out.println("Take " + m); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); }
Exchanger
public static void testExchanger(String[] args) { final Exchanger<byte[]> exchanger = new Exchanger<>(); new Thread(new Runnable() { @Override public void run() { try { byte[] a = "123456".getBytes(); byte[] b = exchanger.exchange(a); System.out.println("123456 ==> " + new String(b)); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); new Thread(new Runnable() { @Override public void run() { try { byte[] a = "456789".getBytes(); byte[] b = exchanger.exchange(a); System.out.println("456789 ==> " + new String(b)); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); }
时间: 2024-10-14 02:26:38