Friday, 25 November 2011

Creating a Thread


A new thread can be created by two ways:

1. By creating  a Thread class : Define a class that extends Thread class and override its run() method with the code required by the thread.

2. By converting a class to thread : Defines a class that implements Runnable interface..The runnable interface has only one method run(), That is to be defined in themethod with the code to be executed by the thread.

Creating thread using Runnable interface

The easiest way to create a thread is to create a class that implements the Runnable
interface. Runnable abstracts a unit of executable code. To implement Runnable, a class need only implement a single method called run( )

To do this consider following steps:

  1. Declare the class as implementing the Runnable interface.

  1. Implement the run() method
public void run( )
Inside run( ), define the code that constitutes the new thread. It is important to understand that run( ) can call other methods, use other classes, and declare variables, just like the main thread can. The only difference is that run( ) establishes the entry point for another, concurrent thread of execution within the program. This thread will end when run( ) returns.

  1. Create a thread by defining an object that is instantiated from this runnable class as a target of the thread

Thread(Runnable threadOb, String threadName)

Here, threadOb is an instance of a class that implements the Runnable interface. This defines where execution of the thread will begin. The name of the new thread is specified by threadName.

     4. Call thread’s start() method to run the thread.      
                   void start( )


Ex,
public class HelloRunnable implements Runnable 
{
 
    public void run() 
    {
        System.out.println("Hello from a thread!");
    }
 
    public static void main(String args[]) 
    {
        (new Thread(new HelloRunnable())).start();
    } 
}
 
 

Extending the Thread class

The second way to create a thread is to create a new class that extends java.lang.Thread, This gives access to all the thread methods directly.

To do this consider following steps:

  1. Declare the class that extends the Thread class.
  2. Implements the run() method that is responsible for executing the sequence of code that the thread will execute.
  3. create a thread object and call the start() method to initiate the thread execution
Ex,
public class HelloThread extends Thread 
{
 
    public void run() 
    {
        System.out.println("Hello from a thread!");
    }
 
    public static void main(String args[]) 
    {
        (new HelloThread()).start();
    }
 
}

Choosing an Approach

The Thread class defines several methods that can be overridden by a derived class. From these methods, the only one that must be overridden is run( ). This is, of course, the same method required when you implement Runnable.

Many Java programmers feel that classes should be extended only when they are being enhanced or modified in some way. So, if  not need to  override any of Thread’s other methods, it is probably best simply to implement Runnable

Posted By : Ruchita Pandya

No comments:

Post a Comment