Digital Edition

SYS-CON.TV
Thread Pooling
Thread Pooling

I don't have to argue the point that the Java language's multi-threading capabilities are great. They're simple to use and generic enough to work on a variety of different implementations. Whether a VM is made to operate using a single operating system thread, like Microsoft's VM, or using native operating system threads for Java threads, like Sun's native threading VM for Solaris, your Java programs will work. I think that's a quiet but powerful feature of Java.

Perhaps it is almost too easy to create and run background threads in Java. Perhaps the designers of Java could have thrown a couple of snafus in there so we programmers didn't go quite so hog-wild sometimes. It can be a problem because a Java VM can't support infinite running threads. In fact, creating a Thread object and starting it running can be very costly in terms of memory.

Don't believe me? Take a look at Listing 1. It's a simple program that just creates as many background Threads as you tell it to on the command-line. Try typing in that program and running it with 1000, or even 10,000, threads. What you should see is an OutOfMemoryError appear after not too long. That's because each background thread requires a lot of memory to run. OutOfMemoryErrors are particularly nasty because there's no verifiable way to recover from them. A program that creates one too many background threads will find itself unable to run and basically will just have to quit.

I really like multi-threaded programs, to the point that I sometimes (not too often) run into this problem. For example, if I use three or four different multi-threaded packages I've created in the past, none of which had an OutOfMemoryError-type problem on their own, I may find the combination of the threads created by the different sub-systems is just too much for a particular VM to handle.

The idea of thread pooling should immediately jump to mind. Thread pooling is when you create a fixed number of background threads and allow your program to use just those finite number of threads, instead of allowing my program objects to create an infinite number of background threads willy-nilly. Usually, thread pooling is used in a client/servant situation. That is, where a servant object (or collection of objects) fulfills any number of simultaneous client requests.

An unconstrained design would have each client request handled by an individual background thread. Listing 2 is an example of an Internet-based service. Each request comes in the form of a client connection to a particular port on the server machine (in this case, port 8888). Listing 2 shows what a main server thread would do: creating a new ClientHandler object and a new Thread for each client request. This main thread is susceptible to being overwhelmed by too many simultaneous client requests. Too many requests and the nasty OutOfMemoryError will appear, probably shutting down the entire server program.

Listing 3 is a ThreadPoolManager class. This class is used to manage a constrained number of threads. Each thread managed by the ThreadPoolManager has a potentially infinite lifetime. Each Runnable object passed to the ThreadPoolManager is queued, and when a thread becomes available to handle the Runnable's task, then that thread is used to run the Runnable's run() method. This way, a huge number of Runnable objects can each have their tasks eventually run without overwhelming the VM by having too many simultaneous running threads. Listing 4 is a replacement for the main thread routine of the server shown in Listing 2. The only change is that the new server uses a ThreadPoolManager's threads instead of creating new threads for each client request.

About Brian Maso
Brian Maso is President of Blumenfeld & Maso, Inc., a Java and Media consulting company. In addition to his involvement in several commercial Java projects, he is also the author of several Java books, including Osborne/McGraw-Hill's upcoming title "Visual J++ From the Ground Up". Brian is the Java guru of DevelopMentor's Java training courses. He has also written several Java white-papers for individual
corporations.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1



ADS BY GOOGLE
Subscribe to the World's Most Powerful Newsletters

ADS BY GOOGLE

Containers and Kubernetes allow for code portability across on-premise VMs, bare metal, or multiple ...
Everyone wants the rainbow - reduced IT costs, scalability, continuity, flexibility, manageability, ...
SYS-CON Events announced today that Silicon India has been named “Media Sponsor” of SYS-CON's 21st I...
We are seeing a major migration of enterprises applications to the cloud. As cloud and business use ...
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22n...
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT...
DXWorldEXPO LLC announced today that "IoT Now" was named media sponsor of CloudEXPO | DXWorldEXPO 20...
Andi Mann, Chief Technology Advocate at Splunk, is an accomplished digital business executive with e...
Most DevOps journeys involve several phases of maturity. Research shows that the inflection point wh...
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by Fi...
Today, we have more data to manage than ever. We also have better algorithms that help us access our...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, ...
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: D...
DevOpsSummit New York 2018, colocated with CloudEXPO | DXWorldEXPO New York 2018 will be held Novemb...
CI/CD is conceptually straightforward, yet often technically intricate to implement since it require...
The now mainstream platform changes stemming from the first Internet boom brought many changes but d...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (No...
CloudEXPO New York 2018, colocated with DXWorldEXPO New York 2018 will be held November 11-13, 2018,...
SYS-CON Events announced today that IoT Global Network has been named “Media Sponsor” of SYS-CON's @...