Java信号量信号量的实现

  

近日于LeetCode看题遇1114年按序打印,获悉一解法使用了信号量,顺势研究,记心得于此。

  

此解视信号为锁,以保证同一时刻单线程的顺序执行。在此原题上,我作出如下更改。

        包装测试;      进口java.util.concurrent.ExecutorService;   进口java.util.concurrent.Executors;   进口java.util.concurrent.Semaphore;      公开课SemaphoreDemo {   静态信号;   静态信号B;   静态信号C;      公共静态void main (String [] args)抛出InterruptedException {=new信号(1);   B=新的信号量(0);   C=新的信号量(0);      ExecutorService=Executors.newFixedThreadPool交货(10);      for (int i=0;我& lt; 7;我+ +){   ex.execute(新R1 ());   ex.execute(新R2 ());   ex.execute(新R3 ());   }   ex.shutdown ();   }      公共静态类R1实现Runnable {   @Override   公共空间run () {   尝试{//A.acquire ();   System.out.println (“1”+ Thread.currentThread () . getname ());//B.release ();   }捕捉(异常e) {   e.printStackTrace ();   }   }   }      公共静态类R2实现Runnable {   @Override   公共空间run () {   尝试{//B.acquire ();   System.out.println (“2”+ Thread.currentThread () . getname ());//C.release ();   }捕捉(异常e) {   e.printStackTrace ();   }   }   }      公共静态类R3实现Runnable {   @Override   公共空间run () {   尝试{//C.acquire ();   System.out.println (“3”+ Thread.currentThread () . getname ());//A.release ();   }捕捉(异常e) {   e.printStackTrace ();   }   }   }      }   之前      

10个线程的常量池中,分别调用R1, R2, R3的方法多次,控制台输出对应各方法名拼接执行该方法的线程名。多次执行结果各不相同:

        1 pool-1-thread-1   2 pool-1-thread-2   1 pool-1-thread-4   3 pool-1-thread-6   2 pool-1-thread-5   3 pool-1-thread-3   1 pool-1-thread-7   2 pool-1-thread-8   3 pool-1-thread-9   3 pool-1-thread-1   2 pool-1-thread-8   1 pool-1-thread-4   3 pool-1-thread-1   1 pool-1-thread-2   2 pool-1-thread-9   1 pool-1-thread-10   3 pool-1-thread-1   2 pool-1-thread-5   1 pool-1-thread-6   3 pool-1-thread-4   2 pool-1-thread-8   之前            1 pool-1-thread-1   2 pool-1-thread-2   3 pool-1-thread-3   1 pool-1-thread-4   2 pool-1-thread-5   3 pool-1-thread-6   1 pool-1-thread-7   2 pool-1-thread-8   3 pool-1-thread-9   1 pool-1-thread-10   3 pool-1-thread-1   1 pool-1-thread-4   2 pool-1-thread-8   3 pool-1-thread-3   2 pool-1-thread-10   1 pool-1-thread-2   2 pool-1-thread-9   3 pool-1-thread-4   1 pool-1-thread-7   3 pool-1-thread-6   2 pool-1-thread-5   之前      

方法能调用,多线程下却无法保证方法的顺序执行。使用信号量后,代码为:

        包装测试;      进口java.util.concurrent.ExecutorService;   进口java.util.concurrent.Executors;   进口java.util.concurrent.Semaphore;      公开课SemaphoreDemo {   静态信号;   静态信号B;   静态信号C;      公共静态void main (String [] args)抛出InterruptedException {=new信号(1);   B=新的信号量(0);   C=新的信号量(0);      ExecutorService=Executors.newFixedThreadPool交货(10);      for (int i=0;我& lt; 7;我+ +){   ex.execute(新R1 ());   ex.execute(新R2 ());   ex.execute(新R3 ());   }   ex.shutdown ();   }      公共静态类R1实现Runnable {   @Override   公共空间run () {   尝试{   A.acquire ();   System.out.println (“1”+ Thread.currentThread () . getname ());   B.release ();   }捕捉(异常e) {   e.printStackTrace ();   }   }   }      公共静态类R2实现Runnable {   @Override   公共空间run () {   尝试{   B.acquire ();   System.out.println (“2”+ Thread.currentThread () . getname ());   C.release ();   }捕捉(异常e) {   e.printStackTrace ();   }   }   }      公共静态类R3实现Runnable {   @Override   公共空间run () {   尝试{   C.acquire ();   System.out.println (“3”+ Thread.currentThread () . getname ());   A.release ();   }捕捉(异常e) {   e.printStackTrace ();   }   }   }      }   

Java信号量信号量的实现