java多线程:并发包中的信号量和计数栓的编程模型
一:信号量的编程模型
1 package com.yeepay.sxf.test.atomic.test; 2 3 import java.util.concurrent.Semaphore; 4 5 /** 6 * 测试信号量 7 * 相当于有一把可以控制并发量的锁。 8 * 例如银行柜台,只有两个窗口。但三个人做业务,只允许同时有两个人能进行做业务 9 * 10 * 多线程 11 * @author sxf 12 * 13 */ 14 public class TestSemaphore { 15 16 public static void main(String[] args) { 17 //声明两个信号量 18 Semaphore semaphore=new Semaphore(2); 19 //有三个线程抢许可证(信号量)做业务 20 Person person1=new Person("sxf", semaphore); 21 Person person2=new Person("sxs", semaphore); 22 Person person3=new Person("ssy", semaphore); 23 //启动这三个线程工作(同时允许的并发量为2) 24 person1.start(); 25 person2.start(); 26 person3.start(); 27 } 28 29 } 30 31 class Person extends Thread{ 32 33 /** 34 * 信号量(许可证) 35 */ 36 private Semaphore semaphore; 37 /** 38 * 当前线程的名字 39 */ 40 private String cname; 41 42 public Person(String cname,Semaphore semaphore) { 43 this.cname=cname; 44 this.semaphore=semaphore; 45 } 46 47 48 @Override 49 public void run() { 50 System.out.println("Person.run(==>)"+getCname()+" is wating........"); 51 try { 52 //获取许可证 53 semaphore.acquire(); 54 System.out.println("Person.run()"+getCname()+" is doneing......."); 55 Thread.sleep(3000); 56 System.out.println("Person.run(==>)"+getCname()+" is service done......"); 57 //释放许可证 58 semaphore.release(); 59 } catch (InterruptedException e) { 60 e.printStackTrace(); 61 } 62 } 63 64 65 public Semaphore getSemaphore() { 66 return semaphore; 67 } 68 69 70 public void setSemaphore(Semaphore semaphore) { 71 this.semaphore = semaphore; 72 } 73 74 75 public String getCname() { 76 return cname; 77 } 78 79 80 public void setCname(String cname) { 81 this.cname = cname; 82 } 83 84 85 86 87 88 89 90 91 92 93 }
一:计数栓的编程模型
1 package com.yeepay.sxf.test.atomic.test; 2 3 import java.util.concurrent.CountDownLatch; 4 /** 5 * 测试记数栓 6 * 7 * 当记数栓定义的多个事件发生时候,才能执行任务 8 * @author sxf 9 * 10 */ 11 public class TestCountDowanLatch { 12 13 public static void main(String[] args) throws InterruptedException { 14 //定义三个事件的计数栓 15 CountDownLatch countDownLatch=new CountDownLatch(3); 16 //定义任务线程 17 Runer runer=new Runer("sxf", countDownLatch); 18 Runer runer2=new Runer("sxs", countDownLatch); 19 Runer runer3=new Runer("sxy", countDownLatch); 20 21 //启动任务线程 22 runer.start(); 23 runer2.start(); 24 runer3.start(); 25 26 //住线程监控特定事件的发生次数 27 for(int i=0;i<3;i++){ 28 Thread.sleep(3000); 29 System.out.println("TestCountDowanLatch.main(事件发生第【"+(i+1)+"】次"); 30 if(i==2){ 31 System.out.println("TestCountDowanLatch.main(事件发生次数已经达标允许线程执行任务)"); 32 countDownLatch.countDown(); 33 } 34 35 } 36 37 } 38 } 39 40 41 class Runer extends Thread{ 42 /** 43 * 计数栓 44 */ 45 private CountDownLatch countDownLatch; 46 47 private String cname; 48 49 public Runer(String cname,CountDownLatch countDownLatch) { 50 this.cname=cname; 51 this.countDownLatch=countDownLatch; 52 } 53 54 @Override 55 public void run() { 56 try { 57 System.out.println("Runer.run()"+getName()+" is await............."); 58 countDownLatch.await(); 59 System.out.println("Runer.run()"+getName()+" is doneing...........cname"); 60 61 } catch (InterruptedException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 } 66 67 public CountDownLatch getCountDownLatch() { 68 return countDownLatch; 69 } 70 71 public void setCountDownLatch(CountDownLatch countDownLatch) { 72 this.countDownLatch = countDownLatch; 73 } 74 75 public String getCname() { 76 return cname; 77 } 78 79 public void setCname(String cname) { 80 this.cname = cname; 81 } 82 83 84 85 }