Skip to content

Commit bb9faf8

Browse files
authored
Update README.md
1 parent 2394429 commit bb9faf8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,25 @@ Thread exp3: 9
299299
Thread exp2: 8
300300
Thread exp3: 27
301301
```
302+
### 12. SynchronizedDemo
303+
Description:
304+
```
305+
/**
306+
* In this example, we have two threads, threadA and threadB, that share a common object lock. threadA enters a synchronized
307+
* block, does some work, then calls lock.wait() to wait for threadB to notify it. threadB, in its synchronized block, does some work,
308+
* and then calls lock.notify() to notify threadA to resume.
309+
* When you run this code, you'll observe that threadA will wait until threadB calls lock.notify(), demonstrating how synchronization
310+
* using synchronized, wait, and notify ensures proper coordination between the two threads.
311+
*/
312+
```
313+
[Code link](https://github.com/wagnerjfr/java_concurrency_algorithms/blob/master/SynchronizedDemo.java)
314+
315+
Sample output:
316+
```console
317+
Thread A is doing some work.
318+
Thread A is waiting for Thread B to notify.
319+
Thread B is doing some work.
320+
Thread B is notifying Thread A to resume.
321+
Thread A is resuming its work.
322+
Both threads have completed.
323+
```

0 commit comments

Comments
 (0)