Keed Thread Alive Java Program

Keed Thread Alive Java Program

In this example you will see how to create standalone java stanalone program runs forever using thread until thread is interrupted. This is very useful if you want to write some backed job using java standalone program which should continuously run without doing much coding:

  • ThreadRunSampleProgram.java:
package com.javahonk.solace;


public class ThreadRunSampleProgram {

    public static void main(String args[]) {
        
    	ThreadRunSampleProgram customSubscriberTest = new ThreadRunSampleProgram();    	
    	
    	customSubscriberTest.run();
    	
    	//Keep thread alive
    	Thread keepAliveThread = new Thread(new Runnable() {

			@Override
			public void run() {
				while (!Thread.interrupted()) {
				}
			}
		});
		keepAliveThread.start();
    }

	private void run(){
		
		System.out.println("I am running");
	}
	
}

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *