Java Thread : run() method


 

Java Thread : run() method

In this section we are going to describe run() method with example in java thread.

In this section we are going to describe run() method with example in java thread.

Java Thread : run() method

In this section we are going to describe run() method with example in java thread.

Thread run() :

All the execution code is written with in the run() method. This method is part of the Runnable interface and the classes which intend to the execute their code. For using this method, first implement the Runnable interface and then define the run() method. We will write all the code here, which we want to execute.

public void run() : By extending the Thread class, run() method is overridden and put all the execution code in this method.

Example :  This example represents the run() method.

public class ThreadRun implements Runnable {

	Thread thread;

	public ThreadRun() {
		thread = new Thread(this);
		thread.start();
	}

	/* Implementing Runnable.run() */
	public void run() {
		System.out.println("run() method executing...");
	}

	public static void main(String args[]) throws Exception {
		new ThreadRun();
		new ThreadRun();
	}
}

Output :

run() method executing...
run() method executing...

Ads