Showing posts with label JVM. Show all posts
Showing posts with label JVM. Show all posts

Sunday, 10 July 2022

What happen to the threads when main method complete execution

As per Java documentation, JVM exits only when all the non-daemon threads complete their execution. Even the main method complete execution, all the non-daemon and daemon threads will continue their operations as long as, there is atleast one non-daemon thread running.

 


What is daemon thread?

Daemon threads are low priority threads, which are mainly used for background processing

 

How to make a thread Daemon?

Signature

public final void setDaemon(boolean on)

 

When you set the setDaemon to true for a thread, then it acts as a daemon thread.

 

How to know whether a thread is daemon or not

isDaemon() method returns true if this thread is a daemon thread; false otherwise.

 

Let’s experiment the same with below example.

 

DaemonThreadsDemo.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

public class DaemonThreadsDemo {

	static void sleep(int noOfSeconds) {
		try {
			TimeUnit.SECONDS.sleep(noOfSeconds);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		Runnable runnable1 = new Runnable() {

			int counter = 0;

			@Override
			public void run() {
				while (counter < 5) {
					counter++;
					sleep(1);
					System.out.println(Thread.currentThread().getName() + " is running");
				}

				System.out.println(Thread.currentThread().getName() + " is exiting");
			}

		};

		Runnable runnable2 = new Runnable() {

			@Override
			public void run() {
				while (true) {
					sleep(1);
					System.out.println(Thread.currentThread().getName() + " is running");
				}
			}

		};

		Thread t1 = new Thread(runnable1, "Non-Daemon thread");

		Thread t2 = new Thread(runnable2, "Daemon thread");
		t2.setDaemon(true);

		t1.start();
		t2.start();

		System.out.println("Main thread finished execution....");

	}

}

 

Output

Main thread finished execution....
Daemon thread is running
Non-Daemon thread is running
Non-Daemon thread is running
Daemon thread is running
Non-Daemon thread is running
Daemon thread is running
Non-Daemon thread is running
Daemon thread is running
Daemon thread is running
Non-Daemon thread is running
Non-Daemon thread is exiting

 

From the output, you can confirm that JVM exits as long as there is no non-daemon thread running.

 

From java doc, A program terminates all its activity and exits when one of two things happens:

 

a.   All the threads that are not daemon threads terminate.

b.   Some thread invokes the exit method of class Runtime or class System, and the exit operation is not forbidden by the security manager.

 

 

  

You may like

Interview Questions

Implement a Closeable iterator in Java

Quick guide to generate random data in Java

What are the different cache eviction strategies?

How to call super class method from sub class overriding method?

Can an enum has abstract methods in Java?

Can enum implement an interface in Java?

Thursday, 13 February 2014

Heap Memory

Heap is a portion of memory where dynamic allocation and de allocations happen

Heap memory is slightly slower to read from and written to, because pointer access is needed to access heap memory

The JVM allocates Java heap memory from the OS and then manages the heap for the Java application.

Heap doesn't have any restrictions on memory size, it totally depends on Hardware.

Possibility of memory leak, if the heap is not organized properly

What are the initial heap size for an Application
Ideally it is platform and JVM dependent.

Initial Heap values for IBM Java
Platform

Initial size
Maximum Size
AIX®
4 MB
64 MB
IBM® i
4 MB
2 GB
Linux®
4 MB
Half the real memory with a minimum of 16 MB and a maximum of 512 MB
Windows®

4 MB
Half the real memory with a minimum of 16 MB and a maximum of 2 GB
z/OS®
1 MB
64 MB

Initial heap size for Oracle Java
On Server class machines
    initial heap size:
    Larger of 1/64th of the machine's physical memory on the machine or some reasonable minimum. Before J2SE 5.0, the default initial heap size was a reasonable minimum, which varies by platform. You can override this default using the -Xms command-line option.

maximum heap size:
    Smaller of 1/4th of the physical memory or 1GB. Before J2SE 5.0, the default maximum heap size was 64MB. You can override this default using the -Xmx command-line option.

Usefule Links




Increase stack size                                                 Increase Heap Size                                                 Home