Other Java Topics
- Interfaces in Java
- Threading and Multithreading in Java
- Introduction to Generics in Java
- Bounded Generics in Java
- Generic Classes in Java
Multithreading is supported in Java. So we can write multithreaded program or applications using Java. A multi-threaded program is made up of two or more component part that can run concurrently independent of each other. These parts can perform different tasks seperatelly without interupting other programs.

Life Cycle of a Java Thread
A thread in java goes through the following life cycle:
New: This is the state of the thread when it is first created. At this state, no program is using it. A thread enters the New state as soon as an instance of the thread is created
Runnable: A thread enters the runnable start as soon as the start() method of the thread is called. This is equivalent to the ready state. It is ready to run but the scheduler has not selected it to run
Running: In the running state, the thread has been selected to run and is actually executing a task.
Waiting(Blocked): In this state, the thread cannot run due to some related factor. For example, it’s waiting for a resource used by another program to be released.
Terminated(Dead): After completing the task, the tread ends its execution and exits. This is the dead or terminated state.
This is shown in Figure 1.0.

Properties of a Thread
One important property of a thread is the priority. The priority of a thread determines the order of schdulling for the thread by the operating system. This is such that threads with higher priorities are scheduled first.
Thread priorities in java have a range from MIN_PRIORITY (1) and MAX_PRIORITY(0). There is also a NORM_PRIORITY (5) which every thread receives by default
How to Create a Thread in Java
There are two methods of creating a thread in Java:
a. By Extending the Thread Class
b. By Implementing the Runnable Interface
a. Extending the Thread Class
In this method, you need to create a class that extends the Thread class which is available in the java.lang.* package. Next, you need to override the run() method. Then you need to create a thread object and call its start method.
//Class MyThread public class MyThread extends Thread { private Thread t; private String ThreadName; //Define a constructor public MyThread(String name) { System.out.println("Thread: " + name + " Created"); } public void run() { try { System.out.println("The thread running now is: " + ThreadName); } catch (Exception e) { System.out.println("Thread: " + ThreadName + " interrupted"); } } public void start() { System.out.println("Started Thread: " + ThreadName); if(t == null) { t = new Thread (this, "t1"); t.start(); } } }
Listing 1.0: Thread Class that extends Thread
import myitems.MyThread; public class RunProgs { public static void main(String[] args) { // TODO Auto-generated method stub MyThread t1 = new MyThread("t1"); t1.start(); MyThread t2 = new MyThread("t2"); try { t2.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } t2.start(); MyThread t3 = new MyThread("t3"); t3.start(); } }
Listing 1.1: Testing Threading Implementation
b. Implementing the Runnable Interface
This is the second way to create a thread in Java. In this method, you create a class that implements the runnable interface. Then you must provide an implementation for the run method and the start methods defined in the runnable interface.
This is shown in Listing 1.2.
public class RunnableThread implements Runnable { public void run() { try { System.out.println("The thread running is: " + Thread.currentThread().getId()); } catch (Exception e) { System.out.println("Error Occured: " + e.getMessage()); } } }
Listing 1.1: Class RunnableThread implements Runnable
Let’s now test this class by creating an object of RunnableThread type and calling the run method
import myitems.RunnableThread; public class RunProgs { public static void main(String[] args) { Thread t1 = new Thread(new RunnableThread()); t1.start(); Thread t2 = new Thread(new RunnableThread()); t2.start(); Thread t3 = new Thread(new RunnableThread()); t3.start(); } }
Listing 1.2: Testing the RunnableThread class
If you run this program, you will have an output as shown in the figure below.


One thought on “Threading and Multithreading in Java”