This tutorial explains you Multithreading in Java.
Multithreading in Java
Introduction
So far you have learned about a single thread. Lets us know about the concept of multithreading and learn the implementation of it. But before that, lets be aware from the multitasking.
Multitasking :
Multitasking
allow to execute more than one tasks at the same time, a task being a program.
In multitasking only one CPU is involved but it can switches from one program to
another program so quickly that's why it gives the appearance of executing all
of the programs at the same time. Multitasking allow processes (i.e. programs)
to run concurrently on the program. For Example running the spreadsheet program
and you are working with word processor also.
Multitasking is running heavyweight processes by a single OS.
Multithreading :
Multithreading is a technique that allows a program or a process to execute many tasks concurrently (at the same time and parallel). It allows a process to run its tasks in parallel mode on a single processor system
In the multithreading concept, several multiple lightweight processes are run in a single process/task or program by a single processor. For Example, When you use a word processor you perform a many different tasks such as printing, spell checking and so on. Multithreaded software treats each process as a separate program.
In Java, the Java Virtual Machine (JVM)
allows an application to have multiple threads of execution running
concurrently. It allows a program to be more responsible to the user. When
a program contains multiple threads then the CPU can switch between the
two threads to execute them at the same time.
For example, look at the diagram shown as:
In this diagram, two threads are being executed having more than one task. The task of each thread is switched to the task of another thread.
Advantages of multithreading over multitasking :
- Reduces the computation time.
- Improves performance of an application.
- Threads share the same address space so it saves the memory.
- Context switching between threads is usually less expensive than between processes.
- Cost of communication between threads is relatively low.
Different states implementing Multiple-Threads are:
As we have seen different states that may be occur with the single thread. A running thread can enter to any non-runnable state, depending on the circumstances. A thread cannot enters directly to the running state from non-runnable state, firstly it goes to runnable state. Now lets understand the some non-runnable states which may be occur handling the multithreads.- Sleeping ? On this state, the thread is
still alive but it is not runnable, it might be return to runnable state later,
if a particular event occurs. On this state a thread sleeps for a specified
amount of time. You can use the method sleep( ) to stop the running
state of a thread.
static void sleep(long millisecond) throws InterruptedException
- Waiting for Notification ? A thread waits
for notification from another thread. The thread sends back to runnable
state after sending notification from another thread.
final void wait(long timeout) throws InterruptedException
final void wait(long timeout, int nanos) throws InterruptedException
final void wait() throws InterruptedException
- Blocked on I/O ? The thread waits for
completion of blocking operation. A thread can enter on this state because
of waiting I/O resource. In that case the thread sends back to runnable
state after availability of resources.
- Blocked for joint completion ? The thread
can come on this state because of waiting the completion of another thread.
- Blocked for lock acquisition ? The thread can come on this state because of waiting to acquire the lock of an object.