Improvements and new concurrency features have been added to Java 7 as part of the JSR 166Y. This topic is part of the Oracle Certified Professional (OCP) Java 7 Certification exam and is a primary objective for the 1Z0-805 Java 7 Upgrade Exam (Beta version was 1Z1-805).
1. Identify potential threading problems
2. Use java.util.concurrent collections
Following have been updated in Java 7:
ForkJoinPool, Phaser, TransferQueue, ConcurrentLinkedDeque, ThreadLocalRandom - provides better performance with less contention than Math.random. e.g. ThreadLocalRandom.current().nextInt(0,100)
3. Use atomic variables and locks
Atomic variables can be defined using atomic types in java.util.concurrent.atomic package. Using atomic variable ensures that operations on that variable are thread safe and no block/method synchronization is required. Examples, AtomicInteger, AtomicLongArray
Instead of synchronized code, Java 7 provides more efficient java.util.concurrent.locks package.
4. Executors and ThreadPools
There are 3 executor interfaces in java.util.concurrent:
- Executor - to launch new tasks
Using Executor, (new Thread(runnable)).start() can be written as executor.execute(runnable)
Instead of starting a new thread, an existing thread may be used from the ThreadPool, thus optimizing performance. - ExecutorService - subinterface of Executor to manage the executor and the service
- ScheduledExecutorService - subinterface of ExecutorService to manage schedule tasks
5. Use the parallel Fork/Join framework
New framework in Java 7 to take advantage of multiple processors on a machine.
No comments:
Post a Comment