What is a Thread:

Let us clarify one thing first:

The name ‘Thread’ actually can appear in two contexts when we are dealing with JEE Application Servers.

Context 1: Thread as in ‘A thread explicitly created by Application Developer from the code (typically by extending the ‘Thread’ class or by implementing ‘Runnable’ interface).
Context 2: The Application Server Threads provided by the Application Server which is configurable via Application Server Admin utilities (mostly Admin Console)
In this lecture, whenever we say Thread, we will be talking about Context 2 (Application Server Threads). So when you are talking to a developer, be clear about this.

Let us define the Thread:

Work enters Application Server via Threads.  Everything the Application Server does is via Threads. For example, when the Application Server first starts, it spawns a Thread to load the Web Application.

Application Server can spawn multiple Threads to execute things simultaneously. For example, when a user is actively performing some work in the application, it usually occupies a Thread. Now, if a new user logs in, the Application Server can spawn a new thread and execute work for the new user using the new Thread.


A thread is also known as LWP – Light Weight Process, It is the basic unit of execution.

A Thread is created by a Process (Heavy weight Process). A process can create multiple Threads (Multi-Threaded Process) or just one Thread (Single Threaded Process). A process at least has one thread which is the PRIMARY Thread.

As you may have guessed, the JVM is a multi-Threaded Process.

Each Thread has its own stack.

All the Threads within a process share the global resources (such as Memory, Operating Systems Resources etc)

Threads in a Java EE Application Server

Remember: The Java EE Application Server (Weblogic, WebSphere etc) are nothing but ‘processes’ as far as Operating System is concerne. Each Application Server instance is a ‘java’ program running in the Operating System. This Application Server Java program is a multi-Threaded program. What is the purpose of this program? It loads and runs the JEE Application written by the developers. Some aspects of the Threads in JEE Application Server (for example: number of Threads) is configurable via the Admin tools provided by the Vendor (ex: Admin Console, wsadmin scripts etc).

Thread Management of one Application Server can differ from another (for example, Jboss and Weblogic)



What is a relationship between the Application Server Threads and the Number of CPUs?

The Server can execute as many Threads simultaneously as the number of CPU cores. Number of cores you have in your server is one of the major factors in determining the number of Threads. But it is not the ONLY factor.

Generally, you should not have to tweak the number of Threads as newer versions of JEE Application servers automatically manage this. Use the following method to determine if you have the optimal number of Threads

  1. Load the application using load testing tools like Jmeter or LoadRunner (or somehow mimic a peak load on the application)
  2. Measure the CPU utilization of the Server (using Operating system tools is fine. For example Windows Task Manager or Unix ‘top’command)
  3. If the CPU is mostly idle, you could technically increase the number of Threads
  4. If the CPU is heavily utilized, you may want to reduce the number of Threads.                     Note: Pay attention to the Memory usage as well as Threads consume memory. The above method is only approximate. Multiple iterations of load testing is required if you need to arrive at the best number.

When do you need to take a Thread dump?

The Thread dump will reveal the activity of all the threads within the JVM process at the time of the Thread dump. It is like an X-Ray for the Application Server. You typically need to take a Thread Dump for following types of issues.

  1. Hung Applications (Applications that do not respond)
  2. Extremely poor response times
  3. Very slow startup
  4. Unusual amount of resource utilization (for example, 100% CPU usage most of the time)
  5. Any time you want to see what the Application Server is doing

Thread Dump:

Thread dump provides a snapshot of the current active live threads. It provides the stack trace of all the java threads in the JVM. It is used when the server is hung and we want to see the threads executing and take their dump.




Comments