Friday 11 November 2016

MULTITHREADING



Process vs. Thread:
1) Both process and Thread is independent path of execution but one process can have multiple Threads.

2) Every process has its own memory space, executable code and a unique process identifier (PID) while every thread has its own stack in Java but it uses process main memory and share it with other threads.

3) Threads are also refereed as task or light weight process (LWP) in operating system

4) Threads from same process can communicate with each other by using Programming language construct like wait and notify in Java and much simpler than inter process communication.

5) Another difference between Process and Thread in Java is that it's How Thread and process are created. It's easy to create Thread as compared to Process which requires duplication of parent process.

6) All Threads which is part of same process share system resource like file descriptors , Heap Memory and other resource but each Thread has its own Exception handler and own stack in Java.

Thread class and method:
     In Java threads are implemented in form of objects that contains a method called run().
     In run() method the entire body and behavior of a thread can be implemented.
     The syntax of run() method is as follows:
public void run()
{
      …….
      …….
}
     A new thread can be created in two different ways:
o   By creating a thread class (i.e. define a class that extends Threadclass )
o   By converting a class into thread (i.e. define a class that implements Runnable interface )
     Extending the Thread class:
o   A class can be made Runnable as thread by extending the class java.lang.Thread. This gives access to all the thread methods directly.
o   It includes the following steps:
§  Declare the class as extending the Thread class:
The Thread class can be extended as follows:
classMyThread extends Thread
{
    …….
    …….
}
§  Implement the run() method:
run() method has been inherited by the class MyThread. When a new thread is started Java calls the thread’s run() method:
public void run()
{
              …….
              …….
}

§  Starting New Thread:
To create and run an instance of a thread the following code must be written:
MyThreadth=new MyThread();
th.start();
The first line creates a new object of class MyThread.
The second line calls the start() method that invokes the thread’s run() method and makes the thread running.

    Example:
class A extends Thread
{
public void run()
 {
for(int i=1;i<=2;i++)
System.out.println("Thread A is executing");
 }
}
classMyThread
{
public static void main(String args[])
 {
A a= new A();
a.start();
 }
} 
Output:
Thread A is executing
Thread A is executing
    Stopping and Blocking a Thread:
o   Stopping a Thread:
A Thread can be stopped by calling its stop()method:
th.stop();
This statement causes the thread to move to dead state. A thread moves automatically to dead state when it reaches the end of its method.
o   Blocking a Thread:
A thread can temporarily suspend or blocked by using one of the following methods:
sleep ()  //blocked for specified time.
suspend () //blocked until further orders.
wait ()      //blocked until certain condition occurs.
The thread will return to Runnable state when specified time elapsed in case of sleep (), the resume () method is invoked in case of suspend (), and the notify () method is called in case of wait

No comments:
Write comments