21 Dec 2014

Creating a Thread in Java

Defining a Thread

In Java, we can define a thread in two ways as:
  1. Extending java.lang.Thread class
  2. Implementing java.lang.Runnable interface
We will look at both ways in this tutorial.

Extending java.lang.Thread

This is the simplest way to define a thread as:
  • Simply extending the java.lang.Thread class
  • Override the run() method to place your task that needs to run by the thread.
This can be shown as:
class SimpleThread extends Thread
{
     public void run()
     {
          System.out.print('Task running by Thread');
     }
}
Thus we have defined a Thread by extending java.lang.Thread class. But, there is a limitation with this approach that if you extend Thread, you can't extend anything else. So, the second way is a good practice for defining a thread as it gives you a way to extend any class you like.

Implementing java.lang.Runnable

The second way for defining a thread is as:
class SimpleRunnable implements Runnable
{
     public void run()
     {
          System.out.print('Task running by Thread');
     }
}

Instantiating a Thread

Regardless of which ever way you choose to define a thread we need a Thread object to do your task. If you have defined a Thread by extending Thread class then instantiation is simple as shown below:

SimpleThread t = new SimpleThread();

But, if you implement Runnable to define a Thread then instantiation will take one more step than above. Rather than combining both the thread and the task into one class, it is splitted into two classes - the Thread class for the thread-specific code and your Runnable implementation class for your task that should be run by a thread code. This is nothing but Thread is the "worker" who will do the given task and the Runnable is the "task" to be done

First we have to instantiate our Runnable class,
SimpleRunnable r = new SimpleRunnable();
 
Now, create an instance(object) of java.lang.Thread class and pass your Runnable instance to the thread.
Thread t = new Thread(r);

Now you've made yourself an instance of a Thread and it know which run() method to call. But, at this point, all we've got is a plain old Java object of type Thread. It is not yet a thread of execution. To get an actual
thread feature we have to start the thread.

Starting a Thread

You've got a Thread instance and starting it is so simple as just invoking start() method on Thread instance as:
t.start();

Note: Make sure that we should call start() method on Thread instance, not on Runnable instance.

The following example shows our complete tutorial for creating a thread.
class SimpleRunnable implements Runnable
{
     public void run()
     {
          System.out.println('Task is running by Thread');
     }
}

public class SimpleThread
{
     public static void main(String args[])
     {
          SimpleRunnable r = new SimpleRunnable();
          Thread t = new Thread(r);
          t.start();
     }
}
On running the above example we will get the following output:
Task is running by Thread
Thus, a Thread executed our task as mentioned in above example. 





Popular Posts

Write to Us
Name
Email
Message