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 (Ten); &nbsP; start.await ();               SYSTEM.OUT.PRINTLN (ID + " start"); thread.sleep (id * 1000); system.out.println ( id + " end"); end.countdown (); } catch (interruptedexception e) { &nbsP; 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) {&nbsP; 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 (Ten); try { for (int i = 0; i < 3; i++) { system.out.println (id + " start "); thread.sleep (id * 1000); system.out.println (ID&Nbsp;+ " end"); barrier.await (); } } catch (interruptedexception | brokenbarrierexception e) { throw new runtimeexception (e); } } }; &nbsP; 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 (Ten); try { semaphore.acquire (); &nbSp; system.out.println (id + " start"); Thread.Sleep (; ) 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 (); &Nbsp; 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); &nbsP; system.out.println ("123456 == > " + new string (b)); } catch (interruptedexception e) { e.printstacktrace (); } } }). Start (); new thread (new runnable () { @Override &nBsp;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 (); }
Java Synchronizer Example