Learn how Java's synchronized keyboard can be used to enforce mutual exclusion on blocks of code with an example Java program in this video.
- [Instructor] Rather than marking an entire method … as synchronized, Java also makes it possible … to synchronize sections of code … with a synchronized statement. … When creating an synchronized statement, … you specify the object that will provide the intrinsic lock. … Before a thread can execute the code contained … within the synchronized statement, … it must first acquire the intrinsic lock … associated with the specified object … and then when the thread is done, it will release its hold … on that lock. … To demonstrate creating a synchronized statement, … I'll modify the data race example program … that I have been using for the past few videos … which has two concurrent threads incrementing … a shared counter variable 10 million times each. … I want to synchronize access to line 11 … which increments the shared garlicCount variable. … So I'll wrap that line in a synchronized statement … and I'll give it the shopper.class object … to use for the intrinsic lock. … When I run this program …
- Parallel vs. sequential computing
- Shared vs. distributed memory
- Execution scheduling
- Thread lifecycle
- Mutual exclusion
- Synchronized methods
- Checking for liveness
Skill Level Advanced
Java Essential Training: Objects and APIswith David Gassner2h 46m Intermediate
Java Essential Training: Syntax and Structurewith David Gassner3h 9m Intermediate
1. Parallel Computing Hardware
2. Threads and Processes
3. Mutual Exclusion
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.