Create a thread

Creating a thread

Java defines two ways by which a thread can be created.

  • By implementing the Runnable interface.
  • By extending the Thread class.

Implementing the Runnable Interface

The easiest way to create a thread is to create a class that implements the runnable interface. After implementing runnable interface , the class needs to implement the run() method, which is of form,

public void run()
  • run() method introduces a concurrent thread into your program. This thread will end when run() returns.
  • You must specify the code for your thread inside run() method.
  • run() method can call other methods, can use other classes and declare variables just like any other normal method.

 

class MyThread implements Runnable
{
 public void run()
 {
  System.out.println("concurrent thread started running..");
 }
}

class MyThreadDemo
{
 public static void main( String args[] )
 {
  MyThread mt = new MyThread();
  Thread t = new Thread(mt);
  t.start();
 }
}

Output :

concurrent thread started running..

To call the run() method, start() method is used. On calling start(), a new stack is provided to the thread and run() method is called to introduce the new thread into the program.


Extending the java.lang.Thread Class


For creating a thread a class have to extend the Thread Class. For creating a thread by this procedure you have to follow these steps:

  1. Extend the java.lang.Thread Class.
  2. Override the run( ) method in the subclass from the Thread class to define the code executed by the thread.
  3. Create an instance of this subclass. This subclass may call a Thread class constructor by subclass constructor.
  4. Invoke the start( ) method on the instance of the class to make the thread eligible for running.

 By extend the Thread class of java.lang package.
 

Extending Thread class Example

This is a way to create a thread by a new class that extends Thread class and create an instance of that class. The extending class must override run() method which is the entry point of new thread.

 

class MyThread extends Thread

{

   public void run()

   {

     System.out.println("Concurrent thread started running..");

   }

}


class MyThreadDemo

{

   public static void main( String args[] )

   {

     MyThread mt = new  MyThread();

     mt.start();

   }

}

 

In this case also, as we must override the run() and then use the start() method to start and run the thread. Also, when you create MyThread class object, Thread class constructor will also be invoked, as it is the super class, hence MyThread class object acts as Thread class object.


What if we call run() method directly without using start() method ?

In above program if we directly call run() method, without using start() method,

public static void main( String args[] )
{
 MyThread mt = new MyThread();
 mt.run();
}

Doing so, the thread won't be allocated a new call stack, and it will start running in the current call stack, that is the call stack of the main thread. Hence Multithreading won't be there.

calling run without start method


Can we Start a thread twice ?

No, a thread cannot be started twice. If you try to do so, IllegalThreadStateException will be thrown.

public static void main( String args[] )
{
 MyThread mt = new MyThread();
 mt.start();
 mt.start();     //Exception thrown
}

When a thread is in running state, and you try to start it again, or any method try to invoke that thread again using start() method, exception is thrown.

Related Articles

post a comment