Java Multithreading


Java Multithreading

A thread can be described as the light-weight smallest part of a process that can run simultaneously with the remaining parts (other threads) of the same process. Threads are not dependent on each other because they all have different path of execution, hence if an exception takes place in one thread, it doesn’t affect the working of other threads. All threads of a process share the common memory. When multiple threads are executed concurrently it is called as multithreading. The main reason of using multithreading is to provide concurrent execution of two or more parts of a program to maximize the CPU utilization time. A multithreaded program contains two or more parts that can run simultaneously, such part of a program is known as a thread. Multithreading is used to utilize the maximum CPU time so that the idle time can be kept to minimum. The two categories of threads are user thread and daemon thread.

Lifecycle of a thread is as shown below:

  • New :  In the new state, a new thread begins its life cycle. It will stay in this state until the program starts the thread. A born thread is another name for it.
  • Runnable:   When a new thread is initiated, it automatically becomes runnable. When a thread is in this position, it is regarded to be doing its job.
  • Timed Waiting:   A runnable thread can be placed in the timed waiting state for a set amount of time. When the period expires or the event it is waiting for occurs, a thread in this state moves back to the runnable state.
  • Terminated (Dead):   When a runnable thread completes its task or otherwise terminates, it is said to be terminated (dead).

Priorities for Threads

Every Java thread has a priority, which aids the operating system in determining the thread scheduling order.

The range of Java thread priorities is between MIN PRIORITY (a constant of 1) and MAX PRIORITY (a constant of 10). NORM PRIORITY is the default priority for all threads (a constant of 5).

Higher-priority threads are more vital to a program and should be given priority over lower-priority threads. On the other hand, Thread priorities cannot ensure the order in which threads execute and are very platform dependant.

Example:

Com.knowledge2life class MultiThread extends Thread{ public void run(){ System.out.println("Running Thread Name: "+ this.currentThread().getName()); System.out.println("Running Thread Priority: "+ this.currentThread().getPriority()); } } public class Main { public static void main(String[] args) { Main multiThread1 = new Main(); multiThread1.setName("First Thread"); multiThread1.setPriority(Thread.MIN_PRIORITY); Main multiThread2 = new Main(); multiThread2.setName("Second Thread"); multiThread2.setPriority(Thread.MAX_PRIORITY); Main multiThread3 = new Main(); multiThread3.setName("Third Thread"); multiThread1.start(); multiThread2.start(); multiThread3.start(); } }

OUTPUT:

Main.java:10: error: cannot find symbol
multiThread1.setName("First Thread");
                        ^
symbol: method setName(String)
location: variable multiThread1 of type Main
Main.java:11: error: cannot find symbol
multiThread1.setPriority(Thread.MIN_PRIORITY);
                        ^
symbol: method setPriority(int)
location: variable multiThread1 of type Main
Main.java:14: error: cannot find symbol
multiThread2.setName("Second Thread");
                        ^
symbol: method setName(String)
location: variable multiThread2 of type Main
Main.java:15: error: cannot find symbolz
multiThread2.setPriority(Thread.MAX_PRIORITY);
                        ^
symbol: method setPriority(int)
location: variable multiThread2 of type Main
Main.java:18: error: cannot find symbol
multiThread3.setName("Third Thread");
                        ^
symbol: method setName(String)
location: variable multiThread3 of type Main
Main.java:20: error: cannot find symbol
multiThread1.start();
                        ^
symbol: method start()
location: variable multiThread1 of type Main
Main.java:21: error: cannot find symbol
multiThread2.start();
                        ^
symbol: method start()
location: variable multiThread2 of type Main
Main.java:22: error: cannot find symbol
multiThread3.start();
                        ^
symbol: method start()
location: variable multiThread3 of type Main