Is there any alternative to synchronize class or method without using 'synchronized' keyword in java?
Is开发者_运维百科 there any alternative to synchronize class or method without using 'synchronized' keyword in java ?
Thanks, Mallikarjun Kokatanur
You may want to look at the changes introduced with the concurrency package, to JDK 5.
http://java.sun.com/developer/technicalArticles/J2SE/concurrency/
Here are some of what is in the article:
- Semaphore: A classic concurrency tool
- CyclicBarrier: A resettable multiway synchronization point (useful for parallel programming)
- CountDownLatch: A utility for blocking until a given number of signals, events, or conditions hold
- Exchanger: Allows two threads to exchange objects at a rendezvous point, and can be useful in pipeline designs
UPDATE The above link is dead but this tutorial is nice, http://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/
Basically, examples of using locks and semaphores.
You could use a "lockable" resource, such as a file.
You can also look at java.util.concurrent.locks which has more sophisticated locking.
Why don't you want to use synchronized?
check out Lock
classes from java.util.concurrent.locks
package, which can be more flexible than synchronized methods.
精彩评论