How do I create a thread in Java?
Author: Deron Eriksson
Description: This Java tutorial shows how to create a Thread in Java by extending java.lang.Thread or implementing the Runnable interface.
Tutorial created using: Windows Vista || JDK 1.6.0_11 || Eclipse JEE Ganymede SR1 (Eclipse 3.4.1)


In JavaSW, we can create a thread by either extending the java.lang.Thread class or by implementing the java.lang.Runnable interface. Although extending the Thread class is slightly easier, in general it is better practice to implement the Runnable interface. There are a couple primary reasons for this. First of all, implementing Runnable allows your class (featuring the run() method) to extend another class. Secondly, if we think in terms of object-oriented design, the code we would like to execute in a separate thread is not really an extension to the functionality of java.lang.Thread itself. Rather, it is simply a job that we'd like to be executed by a thread.

In the first technique, we create a class that subclasses Thread. The run() method contains the code that we'd like to have executed by the thread. Thus, we override Thread's run() method and implement it with our code. We'll call this class ThreadSubclass.

ThreadSubclass.java

package com.cakes;

public class ThreadSubclass extends Thread {

	@Override
	public void run() {
		System.out.println("ThreadSubclass is running");
	}

}

In the second technique, we create a class that implements Runnable and we implement the run() method of Runnable. This method contains our code that will be executed by a thread. This class, RunnableJob, is the job that will be executed by a thread.

RunnableJob.java

package com.cakes;

public class RunnableJob implements Runnable {

	@Override
	public void run() {
		System.out.println("RunnableJob is running");
	}

}

Now, let's create threads using the two techniques. We do this in the ThreadCreationExamples class. In the first technique, we create a ThreadSubclass instance. We start the thread by calling its start() method. In the second technique, we create a RunnableJob instance and then pass this as a parameter to the constructor of a Thread object when we create it. We start this thread by calling its start() method, which will call the run() method of our RunnableJob instance.

ThreadCreationExamples.java

package com.cakes;

public class ThreadCreationExamples {

	public static void main(String[] args) {

		System.out.println("Method 1: extending java.lang.Thread");
		ThreadSubclass thread1 = new ThreadSubclass();
		thread1.start();

		System.out.println("Method 2: implementing java.lang.Runnable");
		RunnableJob runnableJob = new RunnableJob();
		Thread thread2 = new Thread(runnableJob);
		thread2.start();

	}

}

The console output of the execution of ThreadCreationExamples is shown here.

Console Output

Method 1: extending java.lang.Thread
Method 2: implementing java.lang.Runnable
ThreadSubclass is running
RunnableJob is running

Notice in the console output that the two System.out.println() messages from ThreadCreationExamples both precede the System.out.println() messages from both threads that we started, since the execution of this code reached the console before the output from the other two threads.