Package java.util.concurrent

image

JDK 1.5 introduced several new packages and a couple of dozen new classes to beef up support for threads and thread-based systems. These are centered around package java.util.concurrent. In one sentence, the goal is “to do for threads what java.util.Collection did for data structures”. Namely, provide a central, consistent, “bullet-proof” way of expressing best practices, so that programmers no longer need to create them by hand for each new problem.

You can view the concurrent package as a set of design patterns implemented for Java threads (if design patterns are new to you, we mention the basics in the next section). This new package will see the most use in large and sophisticated server systems, and is beyond the scope of this book.

The following is a summary of java.util.concurrency to provide a road map when you are ready to look into it further. The new package contains some utility classes for threads, and some new frameworks.

  • There are a small number of new frameworks built around the interface java.util.collections.Executor. It is now easy to use thread pools (which recycle existing threads instead of creating a new thread for each new request). There is support for delayed and periodic task execution. You can have threads that return results, not just fall off the end of the run() method. You can start a long-running thread, query it for completion, and cancel it.

  • There are some new Collection data classes that you can use when many threads share access to a common data structure. There is a new Queue class called java.util.concurrent.ConcurrentLinkedQueue, which is suitable for holding the collection for producer-consumer, messaging, parallel tasking, and other threads. There are some other concurrent collections that are thread safe but not bottle-necked by a single lock (a single lock undermines scalability). These containers support multiple readers as well as a tunable number of concurrent writes.

  • Improvements in time-related code. There are proper definitions for periods of nanoseconds, microseconds, milliseconds, and seconds. They can easily be converted into one another. There are timeouts for “wait at least this long before giving up”.

  • Synchronization primitives. There are new classes for common synchronization idioms, including a class that represents a counting semaphore.

  • Five new exceptions (e.g. TimeoutException, CancellationException).

The package java.util.concurrent also does some major work on atomicity. That's a performance optimization to support thread safe programs on single variables, without using locks. It's going to take time for all this to sink in, and find its way into the mainstream. It's a very good thing to have sophisticated thread use put on a more formal foundation. The hard work was done by an industry group led by Professor Doug Lea.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.217.253