Menu
Topics Index
...
`


Multithreaded Programming > The Java Thread Model >
Siva Nookala - 20 Feb 2017
There are two ways to create threads.
1. Create Threads implementing Runnable interface
2. Create Threads Extending Thread class

When to use Runnable interface
Use Runnable interface when your class is extending some other class. Since in Java mulitple inheritance is not possible we use Runnable interface to create Threads.
class SomeClass
{
}
class Myclass extends SomeClass implements Runnable
{
    public void run()
    {
        //Write code here    
    }
}
When to extend Thread class
When your class don't extend any other class extend Thread class and override the run method of Thread class and invoke start method to run a thread.
class Myclass extends Thread
{
    public void run()
    {
        // Write code here
    }
}
Creating Threads by implementing Runnable interface
If there is a situation that a class is to be executed as a thread then you can achieve this by implementing Runnable interface. You will need to follow three basic steps:
STEP 1:
Implement run method provided by Runnable interface. This method is executed when a start method is invoked.
Syntax :
public void run( )
STEP 2:
Create a Thread object using any of the following constructors:
Thread(Runnable threadObj, String threadName);
or
Thread(Runnable threadObj);
STEP 3:
Once Thread object is created, you can start it by calling start method, which executes a call to run method.
Syntax :
public void start()
The thread with least priority will be executed first.
The following is a simple program which explains creation of threads using Runnable interface
Thread with Runnable
class RunnableExample
{
    public static void main(String[] args)
    {
    
        Runnable r = new MyThread();
        Thread t = new Thread(r);
        System.out.println("Created thread by implementing Runnable interface.");
        t.start();
    
    }

}

class MyThread implements Runnable
{
    public void run()
    {
        System.out.println("Thread Running...");
    }
}
OUTPUT

Created thread by implementing Runnable interface.
Thread Running...

DESCRIPTION

In this example we created an object of type Runnable. We used the constructor Thread(Runnable obj) to create thread. When thread's start method is invoked it automatically invokes thread's run method.

THINGS TO TRY
  • Created a thread and invoke start method use below code if necessary.
    Thread t1 = new Thread(r);
    t1.start();
  • Change the text "Thread Running..." to "I am run method." in run method and invoke start method on thread.
Create Threads Extending Thead Class
The class which creates threads should extend Thead class and override the run method. The will be much clear when you look at the below example.
Creating Normal Thread
class MyThread extends Thread
{
    public static void main(String[] args)
    {
    
        MyThread thread = new MyThread();
        System.out.println("Created thread by extending Thread");
        thread.start();
    
    }
    
    public void run()
    {
        System.out.println("Thread running...");
    }
}
OUTPUT

Created thread by extending Thread
Thread running...

DESCRIPTION

In the above program MyThread extends Thread so when we create objects for MyThread they behave as threads. When we invoke thread's start method it automatically generates a call to thread's run method.

THINGS TO TRY
  • Create a thread and invoke start method use below code if necessary
    MyThread newThread = new MyThread();
    newThread.start();
  • Place the below code inside the run method and execute the program.
    for(int i = 1; i <= 4; i++)
    {
        System.out.print(i +"\t");
    }

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App