Java concurrency package: Difference between revisions
imported>Prn Dan Mo |
imported>Prn Dan Mo |
||
Line 24: | Line 24: | ||
synchronized method is that only one thread can hold this lock at a time. | synchronized method is that only one thread can hold this lock at a time. | ||
Three most commonly used methods including, wait(), notify(), and notifyAll() are used for | Three most commonly used methods including, '''wait()''', '''notify()''', and '''notifyAll()''' are used for | ||
resource communication between threads. | resource communication between threads. | ||
Revision as of 14:04, 12 August 2010
To provide students with experience in collaboration, you are warmly invited to join in here, or to leave comments on the discussion page. The anticipated date of course completion is 13 August 2010. One month after that date at the latest, this notice shall be removed. Besides, many other Citizendium articles welcome your collaboration! |
The Java concurrency package is a library supporting threading and parallel programming in the Java programming language.
Synchronizers
Mutilthread Synchronization before Java 5
Before Java 5, multithreads are supported using the lock mechanism for synchronization. Locks are implemented in Synchronized method. This mechanism “ensures that only one Java thread can execute an object's synchronized methods at a time” and “also allows threads to wait for resources to become available, and allows the thread that makes resources available to notify other threads that are waiting for the resources”." [1]. When the synchronized keyword is used, the thread which invokes the synchronized method must obtain a lock for the object which makes this thread the lock holder. The rule of thumb of synchronized method is that only one thread can hold this lock at a time.
Three most commonly used methods including, wait(), notify(), and notifyAll() are used for resource communication between threads.
“The wait() method can only be invoked by the object's lock holder. It causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object” [2].
The notify() method wakes up one thread and only the notified thread can go ahead and do something. If there are more than one thread waiting on this object’s waiting queue, one of them is selected to be woke up. notify() wakes up the first thread in the waiting queue.
The notifyAll() method wakes up all threads in the wait set. notifyAll() is normally used when there are many threads to wake up simultaneously. Which thread gets the right to go ahead to execute depends upon thread property such as their priority.
One of the biggest issues of synchronized method is that it is an “all-or-nothing thing” [3]. “Once a thread attempts to enter a synchronized block, it will hang until the lock is available” [3] which causes low performance because all other threads that need the same object have to wait. Another issue is that missing appropriate notifications such as notify() or notifyAll() while programming probably results in deadlock. [4]
Links
References
- ↑ Gary Shute. Java Synchronization. Retrieved on 2010-08-12.