Thread is an execution of a small set of instructions that are independently managed by the operating system scheduler. A Java support multithreading, which means that a simple program is broken parts and every part, is executed concurrently. The parts are model in such a way that it does not affect the running of other parts. It extends multitasking where the application is subdivided into operations called threads. These threads run parallel to each other. The Operating system schedules the processing times of each thread.
- New: New thread’s life cycle begins in a new state and remains in the state until a program calls run method.
- Runnable: It is the executing state of the thread. The thread is said to be in runnable state.
- Waiting: Thread is said to be in waiting state when it waits for other threads’ execution to perform specific task. The Thread can again come into a runnable state when a program calls for it.
- Timed Waiting: Runnable thread enters the waiting state for specific time interval and then can transition back to the executing state after specified waiting time.
- Terminated: When the thread task is completed as required, it gets terminated. However, the thread cannot transition back to the runnable state from this state.
Since the threads run parallel to each other, the priority of threads have to be set by Operating System to determine order of thread execution. Thread priorities is in range between one (MIN_PRIORITY) to ten (MAX_PRIORITY). By default the thread priority is given by NORM_PRIORITY (value five). The thread with a higher priority is executed first i.e. they are allocated processor first.
Threads are created in two ways
- Runnable Interface
- Extending Thread Class
Implementing Runnable Interface
- Step 1: First implement the run() method given by the Runnable interface. run() method is the entry point of thread and complete logic is placed inside this function.public void run()
- Step 2: Instantiate Thread object.Thread(Runnable obj, String threadNam);obj” is the instance of the main class.
- Step 3: After the creation of a thread object you can call start() method which intern calls the run() method to run the thread.void start()
Example using Runnable interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
class RunnableDem implements Runnable { private Thread t1; private String threadNam; RunnableDem( String name) { threadNam = name; System.out.println("Thread Creating " + threadNam ); } public void run() { System.out.println("Thread Running " + threadNam ); try { for(int x = 4; x> 0; x--) { System.out.println("Thread: " + threadNam + ", " + x); Thread.sleep(50); } } catch (InterruptedException e) { System.out.println(" Intruppted Thread " + threadNam + " interrupted."); } System.out.println("Thread " + threadNam + " exit."); } public void start () { System.out.println("Starting " + threadNam ); if (t1 == null) { t1= new Thread (this, threadNam);// New Thread is created t1.start ();//run method is called } } } |
In the main method, the object of the class is created which calls the start method of runnable interface. In the start method a new thread is created and run() method is called for the thread execution.
By Extending the Thread Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
class ThreadDemo extends Thread //Thread class is extended { private Thread t; private String threadNam; ThreadDemo( String name) /// Default Constructor of the Class { threadNam = name; System.out.println("Thread Creating " + threadNam ); } public void run() // Thread execution starts { System.out.println("Thread Running " + threadNam ); try { for(int i = 4; i > 0; i--) { System.out.println("Thread: " + threadNam + ", " + i); Thread.sleep(50);// Sleep method takes the thread in time waiting state for 50 seconds } } catch (InterruptedException e) { System.out.println("Thread " + threadNam + " interrupted."); } System.out.println("Thread " + threadNam + " exit."); } public void start () { System.out.println("Thread Starting " + threadNam ); if (t == null) { t = new Thread (this, threadNam);// thread is created t.start (); } } } class TestThread { public static void main(String args[]) { ThreadDemo t11 = new ThreadDemo( "Thread_1");//Class object is created and Thread name is passed as arguments t11.start(); ThreadDemo t21 = new ThreadDemo( "Thread_2"); t21.start(); } } Output Thread Creating Thread_1 Thread Starting Thread_1 Thread Creating Thread_2 Thread Starting Thread_2 Thread Running Thread_1 Thread: Thread-1, 4 Thread Running Thread-2 Thread: Thread-2, 4 Thread: Thread-1, 3 Thread: Thread-2, 3 Thread: Thread-1, 2 Thread: Thread-2, 2 Thread: Thread-1, 1 Thread: Thread-2, 1 Thread Thread_1 exit. Thread Thread_2 exit. |
The object is created in the main method of the class and the start method is called. The start method creates a thread and calls the run method for thread execution.