What are the two different methods of creating a thread in Java? What method must be implemented in either case?

 

One way of creating a thread in Java is to define a class that is a derived from the Thread class which is built into Java. An object of this derived class will be a thread.

The next question is once we derive from the Thread class, where do we actually do the programming for that thread? Well, the Thread class has a method called run, which you must override in order to have your thread do whatever you want it to do. Suppose we have a class called Derived that extends from the Thread class, then it would look like this:

Example of creating a thread by deriving from the Thread class

public class Derived extends Thread
{
	public void run()
	{
	/* Your implementation of the
	   thread here
	*/
	}
}

Now, if you want to actually create a thread object based on the class above, then you would do something like this:

Derived derivedThread = new Derived();

/* this call will create a new independent thread
   called derivedThread, and will start its processing
*/
   
derivedThread.start();

We’ve now seen that we can create a thread by deriving from the Thread class, so what’s the other way of creating a thread in Java?

Java does not support multiple inheritance, which means that you can not derive from multiple classes at once. So, there may be a situation in which you want to create a thread, but you also want to derive from a class that is not the Thread class. For this reason, Java provides an alternative way of creating a thread – so that you don’t have to create a class that derives from the Thread class. Java provides an interface called Runnable that your class can implement in order to become a thread. The Runnable interface has only one method heading that you must implement yourself:

public void run();

If you create a class that implements the Runnable interface then that class must still be run from an instance of the Thread class. You can accomplish this by passing the Runnable object as an argument to the Thread constructor. If we look at some code this will be much clearer. Suppose we want to create a thread by implementing the Runnable interface, then we would have something like the following:

Example of creating a thread by implementing the Runnable interface

public class ThreadClass extends AnotherClass implements Runnable
{
	...
	public void run()
	{
		/* This is where you would define the behavior of your thread, just
		   as if this class were derived from the Thread class
		*/
	}
	
	public void startThread()
	{
		/* Note below how we pass the current instance of the
		   ThreadClass (by using the "this" parameter) to the 
		   Thread constructor and then calling the run method
		   to actually start the thread
		*/	
		
		Thread aThread = new Thread(this);
		aThread.run();
	}
	
}

You’ve now seen that in order to create a thread you can either extend from the Thread class or implement the Runnable interface. As you’ve probably noticed, in either case you must implement the "run" method.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

2 thoughts on “How to Create a Thread in Java”

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.