Share on Google+Share on Google+

Java Thread : run() method

Ads
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...

Advertisements

Posted on: October 22, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.

Ads

Advertisement