One: The programming model of the semaphore
1 Packagecom.yeepay.sxf.test.atomic.test;2 3 ImportJava.util.concurrent.Semaphore;4 5 /**6 * Test Signal Volume7 * Equivalent to a lock that controls the amount of concurrency. 8 * For example, bank counter, only two windows. But three of people do business, allowing only two people to do business at the same time .9 * Ten * Multithreading One * @authorSXF A * - */ - Public classTestsemaphore { the - Public Static voidMain (string[] args) { - //declare two semaphores -Semaphore semaphore=NewSemaphore (2); + //There are three threads to grab a license (semaphore) to do business -Person person1=NewPerson ("SXF", semaphore); +Person person2=NewPerson ("SxS", semaphore); APerson person3=NewPerson ("Ssy", semaphore); at //start these three threads to work (while allowing concurrent volumes of 2) - Person1.start (); - Person2.start (); - Person3.start (); - } - in } - to classPersonextendsthread{ + - /** the * Semaphore (license) * */ $ PrivateSemaphore Semaphore;Panax Notoginseng /** - * The name of the current thread the */ + PrivateString cname; A the PublicPerson (String cname,semaphore Semaphore) { + This. cname=cname; - This. semaphore=Semaphore; $ } $ - - @Override the Public voidrun () { -System.out.println ("Person.run (==>)" +getcname () + "is wating ...");Wuyi Try { the //obtaining a license - Semaphore.acquire (); WuSystem.out.println ("Person.run ()" +getcname () + "is doneing ..."); -Thread.Sleep (3000); AboutSystem.out.println ("Person.run (==>)" +getcname () + "are service done ..."); $ //Release License - semaphore.release (); -}Catch(interruptedexception e) { - e.printstacktrace (); A } + } the - $ PublicSemaphore Getsemaphore () { the returnSemaphore; the } the the - Public voidSetsemaphore (Semaphore Semaphore) { in This. Semaphore =Semaphore; the } the About the PublicString Getcname () { the returncname; the } + - the Public voidsetcname (String cname) {Bayi This. CNAME =cname; the } the - - the the the the - the the the}View Code
One: Programming model of the Count plug
1 Packagecom.yeepay.sxf.test.atomic.test;2 3 ImportJava.util.concurrent.CountDownLatch;4 /**5 * Test Count plug6 * 7 * A task can be performed when multiple events that are defined by a number plug occur8 * @authorSXF9 *Ten */ One Public classTestcountdowanlatch { A - Public Static voidMain (string[] args)throwsinterruptedexception { - //define a count bolt for three events theCountdownlatch countdownlatch=NewCountdownlatch (3); - //Defining Task Threads -Runer runer=NewRuner ("SXF", Countdownlatch); -Runer runer2=NewRuner ("SxS", Countdownlatch); +Runer runer3=NewRuner ("Sxy", Countdownlatch); - + //Start the task thread A Runer.start (); at Runer2.start (); - Runer3.start (); - - //the number of occurrences that a thread monitors for a particular event - for(inti=0;i<3;i++){ -Thread.Sleep (3000); inSystem.out.println ("Testcountdowanlatch.main" (event occurs "" + (i+1) + "secondary"); - if(i==2){ toSystem.out.println ("Testcountdowanlatch.main (the number of occurrences has been met to allow the thread to perform the task)"); + Countdownlatch.countdown (); - } the * } $ Panax Notoginseng } - } the + A classRunerextendsthread{ the /** + * Counting Plug - */ $ PrivateCountdownlatch Countdownlatch; $ - PrivateString cname; - the PublicRuner (String cname,countdownlatch countdownlatch) { - This. cname=cname;Wuyi This. countdownlatch=Countdownlatch; the } - Wu @Override - Public voidrun () { About Try { $System.out.println ("Runer.run ()" +getname () + "is await ....."); - countdownlatch.await (); -System.out.println ("Runer.run ()" +getname () + "is Doneing...........cname"); - A}Catch(interruptedexception e) { + //TODO auto-generated Catch block the e.printstacktrace (); - } $ } the the PublicCountdownlatch Getcountdownlatch () { the returnCountdownlatch; the } - in Public voidSetcountdownlatch (Countdownlatch countdownlatch) { the This. Countdownlatch =Countdownlatch; the } About the PublicString Getcname () { the returncname; the } + - Public voidsetcname (String cname) { the This. CNAME =cname;Bayi } the the - -}View Code
Java Multithreading: Programming model for semaphore and counter-bolts in a bundle