View difference between Paste ID: U9hVnjzQ and Vcccnu2L
SHOW: | | - or go back to the newest paste.
1-
//---------------------------------
1+
package com.example.foo;
2-
Object lock = new Object();
2+
3-
//Thread A:
3+
import java.util.concurrent.locks.Condition;
4-
synchronized(lock) {
4+
import java.util.concurrent.locks.Lock;
5-
 lock.notifyAll();
5+
import java.util.concurrent.locks.ReentrantLock;
6-
 runBeforeThreadBisNotified();
6+
7-
}
7+
public class LockingDemo {
8-
//-----
8+
9-
//Thread B:
9+
/* What I get:
10-
synchronized(lock) {
10+
11-
 lock.wait();
11+
TEST 1: notify/wait
12-
 runOnNotificationFromThreadA();
12+
Thread notifyB will wait...
13-
}
13+
Thread notifyA notifies all.
14-
//---------------------------------
14+
Thread notifyA: code after notifyAll()
15
... Thread notifyB is awaken.
16-
 final Lock lock = new ReentrantLock();
16+
Thread notifyB: code after wait()
17-
 final Condition cond = lock.newCondition(); 
17+
TEST 2: signal/await
18-
//Thread A:
18+
Thread signalB will await...
19-
 lock.lock();
19+
Thread signalA signals all.
20-
 try {
20+
Thread signalA: code after signalAll()
21-
  cond.signal();
21+
... Thread signalB is awaken.
22-
  runWhenThreadBisFinished() ;
22+
Thread signalB: code after await()
23-
 } finally {
23+
End of Test.
24-
 lock.unlock();
24+
25-
 }
25+
*/
26-
//-----
26+
  public static final void sleep() {
27-
//Thread B:
27+
    try {
28-
 lock.lock();
28+
      Thread.sleep(500);
29-
 try {
29+
    } catch (InterruptedException e) {
30-
  cond.await();
30+
      System.err.println("Interrupted!");
31-
  runWhenSignaled() ;
31+
      Thread.currentThread().interrupt();
32-
 } finally {
32+
    }
33-
 lock.unlock();
33+
  }
34-
 }
34+
35-
//---------------------------------
35+
  public static void main(String[] args) throws InterruptedException {
36
37
    final Lock monitor = new ReentrantLock(true);
38
    final Condition cond1 = monitor.newCondition();
39
40
    final Thread notifyA = new Thread(new Runnable() {
41
42
      public void run() {
43
        sleep();
44
        synchronized (monitor) {
45
          System.out.println("Thread notifyA notifies all.");
46
          monitor.notifyAll();
47
          sleep();
48
          System.out.println("Thread notifyA: code after notifyAll()");
49
        }//now notifyB can really be awaken.
50
      }
51
    });
52
53
    final Thread notifyB = new Thread(new Runnable() {
54
55
      public void run() {
56
        synchronized (monitor) {
57
          try {
58
            System.out.println("Thread notifyB will wait...");
59
            monitor.wait();
60
            System.out.println("... Thread notifyB is awaken.");
61
            sleep();
62
            System.out.println("Thread notifyB: code after wait()");
63
          } catch (InterruptedException e) {
64
            e.printStackTrace();
65
            Thread.currentThread().interrupt();
66
          }
67
        }
68
      }
69
70
    });
71
    System.out.println("TEST 1: notify/wait");
72
    notifyB.start();
73
    notifyA.start();
74
    notifyB.join();
75
    notifyA.join();
76
    sleep();
77
    System.out.println("TEST 2: signal/await");
78
79
    final Thread signalA = new Thread(new Runnable() {
80
81
      public void run() {
82
        sleep();
83
        monitor.lock();
84
        try {
85
          System.out.println("Thread signalA signals all.");
86
          cond1.signalAll();
87
          sleep();
88
          System.out.println("Thread signalA: code after signalAll()");
89
        } finally {
90
          monitor.unlock(); //now signalB can really be awaken.
91
        }
92
      }
93
    });
94
95
    final Thread signalB = new Thread(new Runnable() {
96
97
      public void run() {
98
99
        monitor.lock();
100
          try {
101
            System.out.println("Thread signalB will await...");
102
            cond1.await();
103
            System.out.println("... Thread signalB is awaken.");
104
            sleep();
105
            System.out.println("Thread signalB: code after await()");
106
          } catch (InterruptedException e) {
107
            e.printStackTrace();
108
            Thread.currentThread().interrupt();
109
          } finally {
110
            monitor.unlock(); 
111
          }
112
        }
113
114
    });
115
116
    signalB.start();
117
    signalA.start();
118
    signalB.join();
119
    signalA.join();
120
    sleep();
121
122
    System.out.println("End of Test.");
123
  }
124
125
}