-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSynchronizedDemo.java
55 lines (50 loc) · 2.02 KB
/
SynchronizedDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* In this example, we have two threads, threadA and threadB, that share a common object lock. threadA enters a synchronized
* block, does some work, then calls lock.wait() to wait for threadB to notify it. threadB, in its synchronized block, does some work,
* and then calls lock.notify() to notify threadA to resume.
* When you run this code, you'll observe that threadA will wait until threadB calls lock.notify(), demonstrating how synchronization
* using synchronized, wait, and notify ensures proper coordination between the two threads.
*/
public class SynchronizedDemo {
public static void main(String[] args) {
Object lock = new Object();
// Thread A
Thread threadA = new Thread(() -> {
synchronized (lock) {
try {
System.out.println("Thread A is doing some work.");
Thread.sleep(2000);
System.out.println("Thread A is waiting for Thread B to notify.");
lock.wait();
System.out.println("Thread A is resuming its work.");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Thread B
Thread threadB = new Thread(() -> {
synchronized (lock) {
try {
System.out.println("Thread B is doing some work.");
Thread.sleep(3000);
System.out.println("Thread B is notifying Thread A to resume.");
lock.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Start both threads
threadA.start();
threadB.start();
// Wait for both threads to finish
try {
threadA.join();
threadB.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Both threads have completed.");
}
}