How to create thread in java

Create thread using Runnable Interface


Runnable interface has following specification. It has a abstract method run(). On execution of thread, it execute run() method. So code written in run() method will be executed by newly created thread.

public interface Runnable
 {
  void run();
 }

If programmer is not concerned with control over thread execution then runnable interface is implemented to create thread. After implementation run() method has to be defined.




Step 1 Implement runnable interface and define run method()

Create a class by implementing runnable interface and provide definition for run() method. Let's assume we want this new thread to perform some large calculation. In that case we will write that code in run() method.

Syntax :

class one implements Runnable 
 {
 public void run()
   {
     System.out.println("Thread running...");
   }
 }

Step 2 Create object of Thread class

Create object of Thread class. Thread class has following constructor

  • Thread(Runnable r )
    This constructor needs one Runnable object as argument.

  • Thread (Runnable r, String name)
    This constructor has two parameter. String parameter passed as second parameter will be set as name of thread.

Syntax :

// Using first constructor
Thread th=new Thread(new one());

// Using second constructor
Thread th1=new Thread(new one(), "My-Thread");

Step 3 Call start() method

Finally call start() method. On invocation of start() method a new thread will be created and this thread will execute run() method. Call to start() method is asynchronous which means call to start() method will return immediately.

Syntax :

// Create new thread and start execution
th.start();


Complete Program for multithreading

Following program is having two threads in it. These threads will be running concurrently. Thread scheduling is not predictable. Output of following program is not fixed. It will vary with every execution.

class one implements Runnable 
{
  public void run()
   {
    for(int i=0;i<10;i++)
	 System.out.println("New Thread:"+i);
   }
 }
 class two
 {
  public static  void main(String arg[])
   {
     one ob=new one(); 
     Thread th=new Thread(ob);
	 th.start();
	 
	 // This loop executed by Main Thread
	 for(int i=0;i<10;i++)
	 System.out.println("Main Thread:"+i);
	 
   }  
} 



Review Questions
Q.What is the name of method which has to be defined while extending Thread class.
  1. startThread()
  2. run()
  3. begin()
  4. start()
Answer : b

Q.What is correct way to start a new thread
  1. just create a new Thread object. the thread will start automatically.
  2. Create a new Thread object and call run() method.
  3. Create a new Thread object and call start() method.
  4. Create a new Thread object and call resume() method.
Answer : b

Q.Which statements are true
  1. The Runnable interface has single method named start
  2. The Thread class is abstract
  3. Class Thread implements Runnable interface
  4. Calling the method run on an object implementing Runnable interface will create a new thread
Answer : c, d

Q.What is result of attempting to compile and run the following program
public class myThread extends Thread
{
public myThread(String s)
{
  msg=s;
}
String msg;
public void run()
 {
 System.out.println(msg);
}
public static void main(String arg[])
 {
 new myThread("one");
new myThread("two");
}
}

  1. The program will fail to compile
  2. The program will compile without errors and will print one and two, in that order, every time when program run
  3. The program will compile without errors and will print one and two when run, but order is unpredictable
  4. The program will compile without errors and will simply terminate without any output when run
Answer : d

Q.What will be the result of attempting to compile and run following program
public class demo extends Thread
{
demo(String name)
 {
super(name);
}
public void run() throws IllegalStateException
{
 System.out.println(Thread.currentThread().getName());
throw new IllegalStateException();
}
public static void main(String arg[])
 {
  new demo(" T1 ").start();
}
}

Select one correct answer
  1. Program will fail to compile
  2. Program will compile without error, will print T1 and terminate normally every time the program run.
  3. Program will compile without error, will print T1 and throw IllegalStateException, every time program run
  4. None of the above
Answer : c

Q.What is output of following program
 class DoSomething implements Runnable {
 public void run() {
 System.out.println("Do something");
 }
public static void main(String arg[])
 {
 DoSomething r = new DoSomething();
 Thread t = new Thread(r);
 System.out.println(t.getState());
 }
 }
  1. Do Something
  2. NEW
  3. RUNNABLE
  4. Output is not predictable
Answer : b

Q.What is output of following program
 class DoSomething implements Runnable {
 public void run() {
 System.out.println("Do something");
 }
public static void main(String arg[]) throws InterruptedException
 {
 DoSomething r = new DoSomething();
 Thread t = new Thread(r);
 t.start();
 t.join();
 System.out.print(" End ");
 }
 }
  1. Do something End
  2. End Do Something
  3. End
  4. Output is not predictable
Answer : a

Q.What is output of execution of following program
class DoSomething implements Runnable 
{
 public void run() {
 System.out.println("Do something");
 }
public static void main(String arg[])
 {
 DoSomething r = new DoSomething();
 Thread t = new Thread(r);
 t.start();
 System.out.println(t.getState());
 }
 }
  1. NEW
  2. RUNNABLE
  3. TERMINATED
  4. WAITING
Answer : b