|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
The Java Concurrency Utilities are high-level concurrency types that facilitate threading tasks especially on multicore systems.
Part 1 of this introduction featured java.util.concurrent's Executor framework, synchronizer types, and Java Concurrent Collections package. In Part 2, learn how the Java Concurrency
Utilities handle locking, atomic variables, and fork/join operations. Then prepare for the future with an overview of seven
anticipated changes to the Java Concurrency Utilities coming in Java 8.
Java's low-level threading capabilities are famously hard to use and error-prone, which is why they're frequently associated
with deadlock, thread starvation, race conditions, and other concurrency bugs. One alternative to losing your sleep and peace
of mind is the Java Concurrency Utilities, introduced in JDK 5. The two articles in this short series are dedicated to exploring
how Java developers use the packages and libraries in java.util.concurrent to work around common threading bugs and write cleaner, simpler programs.
In Part 1, I explored the Executor framework, synchronizer utilities, and the Java Concurrent Collections package. Part 2 is an in-depth
look at the mechanics of java.util.concurrent's advanced locking mechanisms and atomic variables, as well as a short tutorial on the Fork/Join framework. I also discuss
the new features and performance improvements coming to the Java Concurrency Utilities with Java 8.
See "Modern threading for not-quite-beginners" (January 2013) for an intermediate primer on multithreaded programming and "Java Tip 144: When to use ForkJoinPool vs ExecutorService" (October 2011) for a quick tip on using the Fork/Join framework.
The Java language lets threads use synchronization to update shared variables safely and ensure that one thread's updates are visible to other threads. In the Java language,
you call synchronization via the synchronized keyword. The Java virtual machine (JVM) supports this mechanism via monitors and the associated monitorenter and monitorexit instructions.
Each Java object is associated with a monitor, which is a mutual exclusion mechanism that prevents multiple threads from concurrently executing in a critical section. Before a thread can enter this section, it must lock the monitor. If the monitor is already locked, the thread blocks until the monitor is unlocked.
Monitors also address the vagaries of memory caching and compiler optimizations that might otherwise prevent one thread from observing another thread's update of a shared variable. Before a thread leaves the critical section, the monitor ensures that the thread's updates are immediately visible, so that another thread about to enter the critical section will see those updates.
Synchronization supports mutual exclusion and visibility. In contrast, the volatile keyword only supports visibility.
Although adequate for simple applications, Java's low-level synchronization mechanism can be inconvenient for advanced applications that require additional capabilities such as timed waits and lock polling.
Previous articles in Java 101: The next generation
java.util.concurrent, with Jeff Friesen's detailed introduction to the Executor framework, synchronizer types, and the Java Concurrent Collections
package.
Concurrency tutorials on JavaWorld:
java.util.concurrent to work around deadlock and similar threading pitfalls.
CountDownLatch and Executors.newFixedThreadPool concurrency utilities.
java.util.concurrent classes were used to optimize thread use for faster performance in a real-world application.
ExecutorService class with ForkJoinPool in a web crawler.