Introduction to Threads in Java

Iroshan Aberathne
2 min readNov 22, 2019

--

After read this article you will be able to get initial understanding on Thread, it life cycle and the implementation of a thread with java code.

Background

Threads are lightweight sub-processes, they share the common memory space. In Multi threaded environment, programs that are benefited from multi threading, utilize the maximum CPU time so that the idle time can be kept to minimum.

Thread Life Cycle

  1. New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
  2. Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
  3. Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
  4. Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
  5. Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.

Creating a Thread

There are two ways to create thread in java. First, using Thread class and other one is using Runnable interface.

  1. Using Thread class
class ThreadDemo extends Thread{
public void run(){
for(int i=1; i<10; i++){
System.out.println(this.getName() + " "+ i);
try{
Thread.sleep(300);
} catch (InterruptedException e){
System.out.println(e);
}
}
}
public static void main(String[] arg){
ThreadDemo thread1 = new ThreadDemo();
thread1.setName("T1");
thread1.start();

ThreadDemo thread2 = new ThreadDemo();
thread2.setName("T2");
thread2.start();
}
}

2. Using Runnable interface

class ThreadDemo implements Runnable{
public void run(){
System.out.println("My thread is in running state.");
}

public static void main(String[] arg){
ThreadDemo obj = new ThreadDemo();
Thread tObj = new Thread(obj);
tObj.start();
}
}

Key Points

  1. Once a particular class extends Thread class, each thread creates a unique object and gets associated with it but in Runnable interface multiple threads share the same objects.
  2. In Java, multiple inheritance not allowed. Hence, if a class extends Thread class, it can not extend any other class but once you implement Runnable interface that barrier can be achieved.
  3. If you only want to specialize run method then implementing Runnable is a better option rather going with Thread class.
  4. Extending Thread class introduces tight coupling as the class contains code of Thread class and also the job assigned to the thread.
  5. Implementing Runnable interface introduces loose coupling as the code of thread is separate form the job of Threads.

Here we are not going to discuss theoretical aspect much, since number of tutorial are available in internet but if you have any doubt please leave comment so that I can response.

Have a nice day !!!!

--

--

No responses yet