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

Having been in the web hosting industry since 2002, dhosting has gained a great deal of experience w...
NanoVMs is the only production ready unikernel infrastructure solution on the market today. Unikerne...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO Silicon Valley 2019 will cover all of these tools, with the m...
SUSE is a German-based, multinational, open-source software company that develops and sells Linux pr...
Your job is mostly boring. Many of the IT operations tasks you perform on a day-to-day basis are rep...
Technological progress can be expressed as layers of abstraction - higher layers are built on top of...
When building large, cloud-based applications that operate at a high scale, it’s important to mainta...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, disc...
Big Switch's mission is to disrupt the status quo of networking with order of magnitude improvements...
Lori MacVittie is a subject matter expert on emerging technology responsible for outbound evangelism...
Dynatrace is an application performance management software company with products for the informatio...
In his session at 21st Cloud Expo, Michael Burley, a Senior Business Development Executive in IT Ser...
All in Mobile is a mobile app agency that helps enterprise companies and next generation startups bu...
Yottabyte is a software-defined data center (SDDC) company headquartered in Bloomfield Township, Oak...
Serveless Architectures brings the ability to independently scale, deploy and heal based on workload...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it wil...
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the c...
"Calligo is a cloud service provider with data privacy at the heart of what we do. We are a typical ...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (No...
Chris Matthieu is the President & CEO of Computes, inc. He brings 30 years of experience in developm...