Multithreading

Aditya
4 min readJun 1, 2021

--

Multithreading in Java is a process of executing multiple threads simultaneously. A thread is a lightweight sub-process, the smallest unit of processing.

• Multiprocessing and multithreading, both are used to achieve multitasking. we use multithreading than multiprocessing because threads use a shared memory area.

• Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such a program is called a thread.

• They don’t allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. Java Multithreading is mostly used in games, animation, etc.

• Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program.

Life Cycle of a Thread:

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.

Following are the stages of the life cycle:

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.

Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transition back to the runnable state only when another thread signals the waiting thread to continue executing.

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.

Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates.

Thread Priorities:

A thread scheduler assigns threads to specific processes based on their priority. A java thread comes with a pre-assigned priority. In addition to this, the java virtual machine can also assign priority to threads or explicitly given by the programmers. The range of values for the priority of a thread lies between 1 and 10. The three static variables associated with priority are −

· MAX_PRIORITY − The maximum priority that a thread has, whose default value is 10.

· NORM_PRIORITY − The default priority that a thread has, whose default value is 5.

· MIN_PRIORITY − The minimum priority that a thread has, whose default value is 1.

getPriority (): method in Java helps in returning the priority of the thread-bound as value to it.

setPriority(): method changes the priority value of a given thread. It throws the Illegal Argument Exception when the thread priority is less than 1 or greater than 10

Thread synchronization:

Java provides a way of creating threads and synchronizing their task by using synchronized blocks. Synchronized blocks in Java are marked with the synchronized keyword. A synchronized block in Java is synchronized on some object. All synchronized blocks synchronized on the same object can only have one thread executing inside them at a time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.

• Generally, threads use their data and methods provided inside their run() methods.

• But if we wish to use data and methods outside the thread’s run() method, they may compete for the same resources and may lead to serious problems.

• Java enables us to overcome this problem using a technique known as Synchronization.

• Illustration.: One thread may try to read a record from a file while another is still writing to the same file.

Syntax:

synchronized(object identifier) {

// Access shared variables and other shared resources

}

Inter-Thread communication:

Inter-thread communication in Java is a technique through which multiple threads communicate with each other. It provides an efficient way through which more than one thread communicates with each other by reducing CPU idle time. CPU idle time is a process in which CPU cycles are not wasted.

When more than one thread is executing simultaneously, sometimes they need to communicate with each other by exchanging information with each other. A thread exchanges information before or after it changes its state

Inter thread communication in Java can be achieved by using three methods provided by the Object class of java.lang package. They are:
1. wait()
2. notify()
3. notifyAll()

1. wait() Method in Java

wait() method in Java notifies the current thread to give up the monitor (lock) and to go into a sleep state until another thread wakes it up by calling notify() method. This method throws Interrupted Exception.

Syntax:

public final void wait()
public final void wait(long millisecond) throws InterruptedException
public final void wait(long millisecond, long nanosecond) throws InterruptedException

2. notify() Method in Java

The notify() method wakes up a single thread called the wait() method on the same object. If more than one thread is waiting, this method will awake one of them.

3.notifyAll() Method in Java

The notifyAll() method is used to wake up all threads that called the wait() method on the same object. The thread having the highest priority will run first.

Syntax :

public final void notifyAll()

References :

https://www.scientecheasy.com/2020/08/inter-thread-communication-in-java.html/

https://www.tutorialspoint.com/java/java_thread_synchronization.htm#:~:text=So%20there%20is%20a%20need,thread%20can%20lock%20or%20unlock

https://geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/

--

--

Aditya
Aditya

Responses (3)