Home Tutorial Java Core Java Thread : run() method

 
 

Java Thread : run() method
Posted on: October 22, 2012 at 12:00 AM
Advertisement
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...

Advertisement

Related Tags for Java Thread : run() method :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 22, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com