在Java的Condition接口【唤醒全部线程】

软件设计 2017-03-25

在Java的Condition接口中,存在的几个方法跟Synchronized中的wait(),waitall(),wait(time ^),这个几个方法一一对应起来,但是在Lock.newCondition()返回中可以指定指定的线程释放和等待资源。

请看【唤醒全部线程】实例:

1 package TestThread.TestCondition;
 2 
 3 import java.util.concurrent.locks.ReentrantLock;
 4 import java.util.concurrent.locks.*;
 5 
 6 public class TestCondition {
 7     public static void main(String[] args) {
 8         ReentrantLock lock = new ReentrantLock();
 9         Condition condition = lock.newCondition();
10         Test1 th1 = new Test1(lock, condition, "线程1");
11         Test1 th2 = new Test1(lock, condition, "线程2");
12         Test1 th3 = new Test1(lock, condition, "线程3");
13         Test2 t2 = new Test2(condition, lock, "我是唤醒线程");
14         th1.start();
15         th2.start();
16         th3.start();
17         try {
18             Thread.sleep(1000);
19             t2.start();
20         } catch (InterruptedException e) {
21             e.printStackTrace();
22         }
23     }
24 }
25 
26 class Test1 extends Thread {
27     private ReentrantLock lock;
28     private Condition condition1;
29 
30     public Test1(ReentrantLock lock, Condition condition1, String name) {
31         super(name);
32         this.lock = lock;
33         this.condition1 = condition1;
34     }
35 
36     @Override
37     public void run() {
38         for (int i = 0; i < 10; i++) {
39             try {
40                 lock.lock();// 加锁
41                 if (i == 5) {
42                     try {
43                         condition1.await();
44                         System.out.println(Thread.currentThread().getName() + "当前线程获取到的值为:++++++++++++" + i);
45                     } catch (InterruptedException e) {
46                         System.out.println(Thread.currentThread().getName() + ":线程被中断了!");
47                     }
48                 } else {
49                     System.out.println(Thread.currentThread().getName() + ":当前线程获取到的值为===》" + i);
50                 }
51             } catch (Exception e) {
52                 System.out.println("系统发生异常!");
53             } finally {
54                 lock.unlock();// 解锁
55             }
56         }
57     }
58 }
59 
60 class Test2 extends Thread {
61     private Condition condition;
62     public ReentrantLock lock;
63 
64     /**
65      * @param condition协作对象
66      * @param lock锁对象
67      * @param name线程名称
68      */
69     public Test2(Condition condition, ReentrantLock lock, String name) {
70         super(name);
71         this.condition = condition;
72         this.lock = lock;
73     }
74 
75     @Override
76     public void run() {
77         try {
78             lock.lock();// 加锁
79             condition.signalAll();;// 唤醒启动全部线程
80             System.out.println(Thread.currentThread().getName() + ":已经唤醒了一个线程!");
81         } catch (Exception e) {
82             System.out.println(Thread.currentThread().getName() + ":线程挂了!");
83         } finally {
84             lock.unlock();// 解锁
85         }
86     }
87 }测试结果:
 

相关推荐