Friday, December 16, 2011

What is Multithreading

Multithreading in Java has many aspects. I will provide a brief introduction to Multithreading with overview of various concepts of Multithreading provided by Java.
Multithreading in Java is based on the concept that multiple execution paths can be started for independent tasks.
Example:
For a birthday party, you may order pizza to one vendor and chocolate cake to other vendor. Since they are to be received from different vendors, you can order them simultaneously with very less time gap. This will help you in receiving your orders much faster than first waiting to receive pizza and then ordering your chocolate cake.
The enterprise applications also use similar kind of behavior to split independent tasks so that the response time for user is reduced. Every thing in java is treated as a thread. Even if there is no user threads executing, there are some background threads running. Also main is also a thread in Java.
Here is a program to illustrated Multithreading:
01
package com.example;
02
 
03
public class MyClass extends Thread{
04
public void run(){
05
System.out.println("MyThread"); 
06
}
07
 
08
public static void main(String args[]) throws InterruptedException{
09
MyClass t1 = new MyClass();
10
t1.start();
11
System.out.println("Main Sleeping"); 
12
Thread.currentThread().sleep(1000);
13
System.out.println("Main walking");
14
}
15
}


Output:
Main Sleeping
MyThread
Main walking

Multithreading in Java involves:
1) Inter-thread communication
2) Synchronization
3) Deadlock
1) Inter-thread communication: When multiple threads are executing then there are cases where one thread needs to communicate with other threads in order to know the current status of other thread so that it can decide upon its execution accordingly. If threads are of same class, they can use instance or static variables for inter-thread communication.
But if threads are created from objects of different classes then wait/notify mechanism is used for inter-thread communication, wait() and notify() methods are defined in object class and these methods are usually invoked on objects which can control the flow (or are common to) of participating threads and not on the threads itself.
2) Synchronization: When there is a mutually exclusive block of code which should be executed by only one thread at a time and which can potentially be executed by multiple threads under normal conditions, synchronization is used.
synchronization is very similar to inter-thread communication done by using wait()/notify() mechanism. The main difference between the two is that synchronization involves executing a common block of code but wait()/notify() involves executing altogether different pieces of code by different threads.
The similarity between the two lies in the fact that one thread is waiting until the other thread hasn’t completed its execution.
3) Deadlock: Deadlock is a situation which arises in an application when no thread can resume its execution because every thread is waiting for other threads to release the resources. In case of a deadlock, no exception or error is thrown but the application can not proceed further. When a deadlock has occurred, the application has to be restarted.
The best way to avoid deadlock is to write better logic for managing threads so that a deadlock situation can be avoided in first place.

No comments:

Post a Comment