condition实现通知部分线程

2018-09-01 05:42:35来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

多个condition实现通知部分线程:

 1 import java.util.concurrent.locks.Condition;
 2 import java.util.concurrent.locks.Lock;
 3 import java.util.concurrent.locks.ReentrantLock;
 4 
 5 public class MyService {
 6     private Lock lock = new ReentrantLock();
 7     //获取两个Condition
 8     private Condition conditionA = lock.newCondition();
 9     private Condition conditionB = lock.newCondition();
10     
11     public void awaitA() {
12         try {
13             lock.lock();
14             System.out.println("begin awaitA时间:" + System.currentTimeMillis() + "----" + Thread.currentThread().getName());
15             conditionA.await();
16             System.out.println("end awaitA时间:" + System.currentTimeMillis() + "----" + Thread.currentThread().getName());
17         } catch (InterruptedException e) {
18             e.printStackTrace();
19         }finally {
20             lock.unlock();
21         }
22     }
23     
24     public void awaitB() {
25         try {
26             lock.lock();
27             System.out.println("begin awaitB时间:" + System.currentTimeMillis() + "----" + Thread.currentThread().getName());
28             conditionB.await();
29             System.out.println("end awaitB时间:" + System.currentTimeMillis() + "----" + Thread.currentThread().getName());
30         } catch (InterruptedException e) {
31             e.printStackTrace();
32         }finally {
33             lock.unlock();
34         }
35     }
36     
37     public void signalAll_A() {
38         try {
39             lock.lock();
40             System.out.println("signalAll_A时间为:" + System.currentTimeMillis() + "----" + Thread.currentThread().getName());
41             conditionA.signalAll();
42         } catch (Exception e) {
43             e.printStackTrace();
44         }finally {
45             lock.unlock();
46         }
47     }
48     
49     public void signalAll_B() {
50         try {
51             lock.lock();
52             System.out.println("signalAll_B时间为:" + System.currentTimeMillis() + "----" + Thread.currentThread().getName());
53             conditionB.signalAll();
54         } catch (Exception e) {
55             e.printStackTrace();
56         }finally {
57             lock.unlock();
58         }
59     }
60 }
 1 public class ThreadA extends Thread{
 2 
 3     private MyService service;
 4 
 5     public ThreadA(MyService service) {
 6         this.service = service;
 7     }
 8     
 9     @Override
10     public void run() {
11         service.awaitA();
12     }
13 }
 1 public class ThreadB extends Thread{
 2 
 3     private MyService service;
 4     
 5     public ThreadB(MyService service) {
 6         this.service = service;
 7     }
 8     
 9     @Override
10     public void run() {
11         service.awaitB();
12     }
13 }
 1 /**
 2  *    测试类
 3  */
 4 public class Run {
 5 
 6     public static void main(String[] args) {
 7         MyService service = new MyService();
 8         ThreadA a = new ThreadA(service);
 9         a.setName("A");
10         a.start();
11         
12         ThreadB b = new ThreadB(service);
13         b.setName("B");
14         b.start();
15         try {
16             Thread.sleep(3000);
17         } catch (InterruptedException e) {
18             e.printStackTrace();
19         }
20         //只唤醒A
21         service.signalAll_A();
22     }
23 }

运行结果如下:

  

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:spring-构建mvc工程

下一篇:SpringMVC框架六:拦截器