Search This Blog

Sunday, February 20, 2011

Introduction to Thread in Java

What is Thread?

Thread: light weight process which can access the resource of the process that creates it. Creating a thread is more efficient than creating a new process. Multi-threaded programs utilize the CPU better than single thread programs. Therefore, multi-threading /concurrency is an important feature in Java.

Thread is efficient because multiple threads can share the same resources. However, this also creates problems in communication between threads and the integrity of the resources they share.

In Java, the Thread class and the Runnable interface are used to make classes executable in threads. In order words, if we want our class to be executed in a separate thread, we must either:

  • Subclass the Thread class
  • Or implement the Runnable interface

Defining threadable classes by subclassing the Thread class

We can make our class threadable by subclassing the Thread class like this:

public class ThreadableClass extends Thread
{
  public void run()
  {
    //code to be executed in thread...
  }
}

Next, the only thing that we must do is to override the run() method in our class. The reason is that when the objects of our class are executed as threads, the run() method is called immediately and only code inside this method is executed.

To use our threadable class, we can just instantiate it like any other class. For example:

ThreadableClass newThread = new ThreadableClass();

Defining threadable classes by implementing the Runnable interface

In this method, to create a new thread, we just need to create a new Thread object by calling the Thread constructor. There are several thread constructors. You can find them here Java Thread API. We'll focus only on the most common ones which are Thread(Runnable target) and Thread(Runnable target, String name).

Thread(Runnable target) creates a new thread that contains the "target" runnable object. This means that whenever the thread starts, the object is immediately executed in that thread.

Thread(Runnable target, String name) not only creates a new thread containing the "target" object but it also accepts a name which you give to it.

Here is an example of how to create a thread and give it a name:

Thread newThread = new Thread(myObject, "Thread 1");

Similar to subclassing the Thread class, the class that implements the Runnable interface must also implement the run() method. That method is called when a thread starts. Therefore, any code to be executed in the thread must be inside the run() method. In addition, the thread stops when the run() method returns. It is important to remember that when implementing the run() method.

Here is how to implement the Runnable interface:

public class RunnableClass extends ParentClass implements Runnable
{
  public void run()
  {
    //code to be executed in thread...
  }
}

Notice that our RunnableClass is now a runnable class while still able to inherit methods from its ParentClass.

To use our runnable class, we must instantiate the Thread object and pass in the object of our runnable class as an argument. Whenever the Thread object starts, our object will be executed inside that thread. For instance:

Thread runnableThreadClass = new Thread(runnableClassObject, threadName);

Implement Runnable vs. subclass Tread

So which method is better? In general, implementing the Runnable class is the way to go because it gives our classes better flexibility. For instance, once our classes extend the Thread class, they cannot inherit from any other class because Java doesn't support multiple, direct inheritance.

However, when writing simple applications that utilize threads, subclassing the Thread class is faster and easier to use.

That is it for this first installment. Here is the link to the second post of the series "Thread Life Cycle".

No comments:

Post a Comment