Bootstrap FreeKB - Java - What is a process and what is a thread in Java on Linux
Java - What is a process and what is a thread in Java on Linux

Updated:   |  Java articles

What is a process?

A process contains numerous threads, so before we get into threads, it good to understand what a process is. A process is a program running on the operating system and each process has a unique process identification number (PID). In this way, processes are independent and isolated from each other. The ps command can be used to view the processes that are running. Here is an example of the first 5 processes as identified by the ps command.

~]# ps -ef
UID      PID  PPID  C STIME TTY      TIME CMD
root       1     0  0 Mar19 ?    00:00:00 systemd
root       2     0  0 Mar19 ?    00:00:00 kthreadd
root       3     0  0 Mar19 ?    00:00:00 ksoftirqd
root       4     0  0 Mar19 ?    00:00:00 kworker
root       5     0  0 Mar19 ?    00:00:00 migration

 

What is a thread?

A process will contain numerous threads. Unlike processes, threads are not totally independent and isolated from each other. For example, different threads can use the same memory space and variables.

Let's consider a sceario where there is a java process created by a Java Virtual Machine (JVM), such as JBoss, Tomcat, or WebSphere. In this scenario, one possible thread would be when an application in the JVM makes a database query. 

 

main() thread

Every Java program has at least one thread, the main thread, called by the main() method. There will most certainly be other threads, such as the JVMs garbage collector thread. The important thing to recognize is that a Java program will always create threads.

 

Take for example the following markup. Notice public static void main(String bar[]). This creates the main() thread.

package com.example;

public class foo {

  pubic static void main(String bar[]) {
    System.out.println("Hello World");
  }
}

 

Watching threads in real time

If you have an application deployed to a WebSphere application server, you can use the WebSphere admin console to watch the threads in real time. In this example, the graph shows a spike when threads in the WebContainer thread pool are in use.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter db2ee3 in the box below so that we can be sure you are a human.