From the course: Advanced Java: Threads and Concurrency
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Implementing synchronization - Java Tutorial
From the course: Advanced Java: Threads and Concurrency
Implementing synchronization
- In the run method of MyRunnable here, from line 14 to 16 you can see the syntax of using the synchronized keyword on a code block within a method. See what's being passed here into the synchronized block. It is the monitor object. In this example, the monitor object is the this or current object. In other words, it says, use the current object's monitor as the monitor object for synchronizing the block of code. It doesn't always have to be this. It can be any object's monitor on which you want to synchronize the code. On the other hand, if it was a synchronized method, like in line 24, the monitor object is implicitly the monitor of the current object to which the method belongs as it is an instance method. In either case, we are talking about an instance or object level lock. That is, the critical code is protected by the monitor of a given object. This means that other objects can still access the synchronized code. If class-level synchronization is needed, the synchronized…
Contents
-
-
-
Memory access in Java threads and its problems5m 43s
-
(Locked)
Memory inconsistency: Data race5m 40s
-
(Locked)
Thread interference: Race condition5m 38s
-
(Locked)
Synchronization: Purpose and use3m 53s
-
(Locked)
Implementing synchronization3m 15s
-
(Locked)
Challenge: Inventory manager2m 45s
-
(Locked)
Solution: Inventory manager4m 50s
-
-
-
-
-
-
-
-