Java Synchronizer Example

Source: Internet
Author: User
Tags semaphore

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.