java多執行緒:併發包中的訊號量和計數栓的程式設計模型

Love Lenka發表於2016-09-25

一:訊號量的程式設計模型

 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 }
View Code

一:計數栓的程式設計模型

 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 }
View Code

 

相關文章