Memory representation of thread creation in java

In a recent post, we have learned what is a thread in java, the creation thread in java, and ways to create thread in java. In this post, we will see how does JVM works in memory after thread creation in java? What happens in memory after thread creation in java or thread execution?

Here is the table content of the article will we will cover this topic.
1. How does thread creation work in java?
2. How do multiple threads work?
3. How does thread work if an exception occurs?

How does thread creation work in java?

Each java application has at least one thread, it can have more than one thread that works concurrently. The JVM allocates a stack to each thread, that is used for storing runtime data. When JVM starts the execution from the main method it creates a stack for the main thread and stores the data in the particular stack.

All the method calls store in the stack and each entry is known as a stack frame. A stack can have several stack frames that totally depend upon the stack size. If a thread tries to store more items on the stack than the stack size allows, the thread will throw a stack overflow error.

Let us see how the main thread is created by JVM and how does it work.

public class JavaGoal
{
	public static void main(String args[]) 
	{
		System.out.println("Main method");
		JavaGoal obj = new JavaGoal();
		obj.firstMethod();
		obj.secondMethod();
	}
	
	public void firstMethod()
	{
		System.out.println("First Method");
	}
	
	public void secondMethod()
	{
		System.out.println("Second Method");
	}
}

Output: Main method
First Method
Second Method

In the above example, we have the main method and two other methods(firstMethod and secondMethod). The JVM starts the execution from the main method and creates a stack for the main thread. The other methods are getting calls from the main method and created a new stack frame for each method call.

thread creation in java

1. JVM creating a stack for the main thread.
2. The main method calling the firstMethod() and the JVM creates an entry as frame stack.
3. Again, the main method calling the secondMethod() and the JVM creates another frame stack in the stack.

After the execution of each method, its corresponding entry(stack frame) will be removed from the stack. When the stack is empty then the run-time stack is destroyed by the JVM.

thread creation in java

How do multiple threads work?

As we know an application can have any number of threads. The JVM always runs the main thread first because it is the entry point of the program. All the other threads must be part of the main thread it means each thread is start execution after the main thread. Suppose we have multiple threads in a program then JVM will create multiple stacks at run time.

1. The JVM creates a runtime stack for each thread.
2. Each thread has a separate stack and that stores the data of the particular thread.
3. Each call stores in the stack as a separate entry known as a stack frame.
4. After completion of each call, the corresponding stack frame removed from the stack.
5. After completion of all calls, the stack will be empty, and the JVM automatically destroyed the
stack.

class ExampleOfThreadCreation extends Thread
{  
   public static void main(String args[])
   {  
	   firstMethod();
	   SecondMethod();
       ExampleOfThreadCreation t1 = new ExampleOfThreadCreation();
       t1.start();
   }  
   public void run()
   {
	   System.out.println("Thread is running...");
	   for(int i = 0; i < 5 ; i++)
		   methodOfThread(i);
	  
   }
   
   public static void firstMethod()
   {
	   System.out.println("First method");
   }
   
   public static void SecondMethod()
   {
	   System.out.println("Second method");
   }
   
   public static void methodOfThread(int i)
   {
	   System.out.println("Counting in thread : " + i);
   }
}  

Output: First method
Second method
Thread is running…
Counting in thread : 0
Counting in thread : 1
Counting in thread : 2
Counting in thread : 3
Counting in thread : 4

thread creation in java

How does thread work if an exception occurs?

The thread does work differently if an exception occurs in the program. The JVM handles it step by step and recover the exception if exception handling code is present.

NOTE: If you are not familiar with Exception handling then read it first.

1. Suppose a NullPointerException is occurring in the method, then the JVM checks the exception handling code is there. If code is not presented, then JVM creates an object of exception and remove the current frame from the stack.

2. After that JVM checks the caller method if it is having any exception handling code is not. If code is not presented there, JVM terminates the execution and remove the current frame from the stack.

3. The above process continues until the JVM reaches on the main thread. If the main thread doesn’t have exception handling code, then JVM terminates the execution and print the exception by use of default exception handler.

class ExampleOfThreadCreation extends Thread
{  
   public static void main(String args[])
   {  
	   firstMethod();
   }  
  
   public static void firstMethod()
   {
	   System.out.println("First method");
	   SecondMethod();
   }
   
   public static void SecondMethod()
   {
	   System.out.println("Second method");
	   int a = 2/0;
   }
}  

Output: First method
Second method
Exception in thread “main” java.lang.ArithmeticException: / by zero at ExampleOfThreadCreation.SecondMethod(ExampleOfThreadCreation.java:17)  at ExampleOfThreadCreation.firstMethod(ExampleOfThreadCreation.java:11) at ExampleOfThreadCreation.main(ExampleOfThreadCreation.java:5)

thread creation in java

1. The above example, a NullPointerException is occurring at method secondMethod(). The JVM will check whether the exception handling code is presented or not. If not, then JVM moves to the caller method and remove the current stack frame from the stack.

2. In second step the JVM is checking the exception code in the firstMethod() (the caller method()). If it’s not having exception handling code, then it removes the current stack frame from the stack.

3. At the last, the JVM checks the main thread(main method) if have any exception handling code is present or not. If not, then terminates the main method abnormally and the default exception handler is responsible to print the exception message to the output screen which is the part of JVM.

3 thoughts on “Memory representation of thread creation in java”

  1. Consider three threads are running, If one thread encounters a error or exception,
    what happens to the execution of remaining threads?
    Will all the remaining threads also stopped the execution?

    Reply

Leave a Comment