Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, you have to decide which JDK are you using. The first Java releases where only provided of Threads. Since the Java Tiger (5.0), new classes have been introduced to handle concurrency. In particular, a complete package has been provided, the <strong>java.util.concurrent</strong>.</p> <p>In my experience, I found that monitors are better, since they let the code more clean. Moreover, using them let the code more <em>easy-to-understand</em>. They are generally implemented through a class that implements the <strong>Lock</strong> interface: the most famous implementations provided by the JDK are the <strong>ReentrantLock</strong> class, which defines a general lock, and the <strong>ReentrantReadWriteLock</strong> class, that provides a specific <em>write</em> and/or <em>read</em> lock.</p> <p>Threfore, a lock is used to enable/disable the access to a shared object (e.g. a list of objects).</p> <p>A <strong>Semaphore</strong> object is a synchronized used to coordinate and control the threads (there are many synchronizers provided in the latest JDKs, like <em>Semaphore</em>, <em>CyclicBarrier</em>, <em>CountdownLatch</em>, and <em>Exchanger</em> classes). For instance, with a Semaphore you can release a fixed number of tokens to your pool of Threads and so decide the amount of operations that can be executed simultaneously. Personally, I don't like this approach, since the use of a pool of thread with Futures and Locks lead to the same result in a cleaner and safer way.</p> <p>More info can be found in this book: "<a href="http://jcip.net/" rel="nofollow">Java Concurrency in Practice</a>" and in this IBM's tutorial: "<a href="http://www.ibm.com/developerworks/java/tutorials/j-concur/" rel="nofollow">Concurrency in JDK 5.0</a>". Some more nice examples can be found <a href="http://www.javaconcurrencyinpractice.com" rel="nofollow">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload