| By Gaurav Khanna | Article Rating: |
|
| October 7, 2009 08:41 PM EDT | Reads: |
3,323 |
Thread Pooling in Java 5
Thread Pooling in Java 5 is configured using one of the implementations of the executor interface.
One implementation is ThreadPoolExecutor. This implementation, as the name, suggests is configurable with the no of threads in the pool, the maximum pool size, a rejection handler, a thread factory, a work queue.
Executor <---------- ExecutorService <-------- AbstractExecutorService <----------- ThreadPoolExecutor <---------- ScheduledThreadPoolExecutor (This is the depiction of the hierarchy => Executor is extended by the ExecutorService interface which is realized by the AbstractExecutorService class and so on)
The significance of these configurable parameters is listed below barring the self explanatory parameters:
- Rejection Handler / Saturation Policies: This is an implementation that determines the policy to be used in cases when a task is to be rejected. The rejection could be because of many reasons: the work queue is filled up, the executor is shutting down etc. There are predefined policies as well that are detailed in the JavaDocs.
- Thread Factory: An implementation of this will be used by the executor (ThreadPoolExecutor in this case) to create the pool thread. This is very useful since a customized thread factory implementation can be setup to perform a variety of customizations. Such as: specifying an UncaughtExceptionHandler on a per thread basis.
- Work Queue: This is where the tasks that are to be executed are held at. One could configure the work queue to be of a certain size. The pool threads will pop off this work queue and execute the tasks based on a certain policy that in turn is based on the BlockingQueue implementation. For instance: if the work queue is a PriorityQueue then tasks will be popped off based on priority.
A ScheheduledThreadPoolExecutor can additionally be setup to execute tasks periodically. A task can be scheduled to execute after a given delay and at a specified interval thereafter. If a task takes longer than the interval specified to execute then another thread is spawned or reused from the pool to execute the task. This is different from a Timer in terms of the task always is executed after the interval even if the preceding task is still running.
Difference between executor obtained from Executors and directly instantiating the ThreadPoolExecutor or ScheduledThreadPoolExecutor
The distinguishing facet is that the Executor obtained from the new****Thread*** methods in Executors is preconfigured and cannot be reconfigured while it is running. Of course one can cast it to ThreadPoolExecutor (or any other implementation) and then invoke it's configuration API but it is discouraged since that would require coupling the caller and the callee.
An Executor so obtained is preconfigured in terms of:
. Saturation Policies
. Thread Factory
. Work Queue
In order to customize, one would need to instantiate the appropriate Executor such as ThreadPoolExecutor or ScheduledThreadPoolExecutor.
Published October 7, 2009 Reads 3,323
Copyright © 2009 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Gaurav Khanna
I have 12 years of Java with special emphasis on the server and have architected and designed many systems and components. I have been employed as lead engineer at Hyperion Solutions in the Common Technology / Platform group (acquired by Oracle corp). I have been employed at Apple most recently and Hyperion prior to that. I have a Bachelors in Technology in Computer Science and Engineering. I have led teams and mentored engineers in my earlier assignments as well. Have run a 6:30 mile, practiced boxing and jiu-jitsu, am a regular at the gym and like the outdoors. I would like to continue in technology perhaps moving on to establishing a tech startup. You would find me here or at my blog: http://khanna111.com/wordPressBlog
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Cross-Platform Mobile Website Development – a Tool Comparison
- Three Buzzwords That Every CIO Hears but One They Should Listen To
- Write Once Run Anywhere or Cross Platform Mobile Development Tools
- Immersing into JavaScript Frameworks
- Workday Reportedly Prepping to Go Public
- Cloud Expo New York: The Java EE 7 Platform - Developing for the Cloud
- Book Review: Sams Teach Yourself Java in 24 Hours
- OpenOffice.com Lives
- Book Excerpt: Introducing HTML5
- Adobe Sends Flex to the Apache Foundation
- Five Years Waiting for JRE 7: Is It Justified? (Part 1)
- Book Excerpt: Java Application Profiling Tips and Tricks
- i-Technology in 2012: Five Industry Predictions
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- OpenXava 4.3: Rapid Java Web Development
- The Next Web Architecture
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Is Write Once Run Anywhere Ever Going to Be a Reality?
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- JavaServer Faces (JSF) vs Struts
- The i-Technology Right Stuff
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- What's New in Eclipse?
- i-Technology Predictions for 2007: Where's It All Headed?


















