Presentation On:: Multi-Threading and Thread Synchronization

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Presentation on:

MULTI-THREADING AND
THREAD SYNCHRONIZATION

SUBMITTED BY: Juhi Kumari


Multithreading in Java
 Multithreading in java is  a  process  of  executing 
multiple threads simultaneously.
 Thread  is  basically  a  lightweight  sub-process,  a 
smallest  unit  of  processing.  Multiprocessing  and 
multithreading, both are used to achieve multitasking.
 But  we  use  multithreading  than  multiprocessing 
because  threads  share  a  common  memory  area.  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.
Advantages of Java Multithreading

1)It doesn't block the user because  threads  are 


independent and you can perform multiple operations at 
same time.

2)  You can perform many operations together so it


saves time.

3)  Threads  are independent so  it  doesn't  affect  other 


threads if exception occur in a single thread.
What is Thread in java

A  thread  is  a  lightweight  sub  process,  a 


smallest  unit  of  processing.  It  is  a  separate 
path of execution.

Threads  are  independent,  if  there  occurs 


exception  in  one  thread,  it  doesn't  affect 
other  threads.  It  shares  a  common  memory 
area.
Life cycle of a Thread (Thread States):-

 A thread can be in one of the five states. According


to sun, there is only 4 states in thread life cycle in
java  new,runnable,non-runnable and terminated.
There is no running state. But for better
understanding the threads, we are explaining it in
the 5 states.
 The life cycle of the thread in java is controlled by
JVM.
New :
When a thread is created by new statement but not yet
run, it is in newborn state.

It is also referred to as a born thread.

In this state, the local data members are allocated and
initialized.

Runnable :
 The runnable state means that a thread is ready to run and
is waiting for the availability of the processor.

In other words, threads are in this state in a queue and


wait their turns to be execution.
RUNNING STATE:-

 Running means that the thread has control on the


processor.

 The thread is in running state if the thread


scheduler has selected it.

 System assigns processor to thread (thread begins


executing).

 When run completes or terminates, enters dead


state.
Non-Runnable (Blocked):-

This is the state when the thread is still alive, but is


currently not eligible to run.

 A thread can enter in this state because of waiting the


resources that are hold by another thread.

Terminated:-

A thread can be considered dead when its run() method


completes.

If any thread comes on this state that means it cannot


ever run again.
SYNCHRONIZATION
 Synchronization is the capability to control the
access of multiple threads to any shared
resource.
 It is a better option if we want to allow only
one thread to access a shared resource at a
particular time.
 It is mainly use to prevent thread interference.
Synchronization can be implemented in
two ways in Java:

 Mutual Exclusion.
 Inter- Thread Communication(Cooperation)
MUTUAL EXCLUSION

Mutual Exclusive helps keep threads from interfering with one


another while sharing data. This can be done by three ways in
java:

by synchronized method


by synchronized block
by static synchronization
JAVA SYNCHRONIZED METHOD

 If you declare any method as synchronized, it is known as


synchronized method.
 Synchronized method is used to lock an object for any shared
resource.
 When a thread invokes a synchronized method, it automatically
acquires the lock for that object and releases it when the thread
completes its task.

Syntax:
Synchronized return_type method_name()
{//block}
JAVA SYNCHRONIZED BLOCK

 Synchronized block can be used to perform synchronization on any


specific resource of the method.
 Suppose you have 50 lines of code in your method, but you want to
synchronize only 5 lines, you can use synchronized block.
 If you put all the codes of the method in the synchronized block, it
will work same as the synchronized method.
 Points to remember for Synchronized block
 Synchronized block is used to lock an object for any shared
resource.
 Scope of synchronized block is smaller than the method.
Syntax to use synchronized block

synchronized (object reference expression)
 {   
  //code block   
}  
STATIC SYNCHRONIZATION

 If you make any static method as synchronized, the lock


will be on the class not on object.
class Table{   class MyThread2 extends Thread{  
 synchronized void printTable(int n) Table t;  
{//synchronized method   MyThread2(Table t){  
   for(int i=1;i<=5;i++){   this.t=t;  
     System.out.println(n*i);   }  
     try{   public void run(){  
      Thread.sleep(400);   t.printTable(100);  
     }catch(Exception e) }  
        {System.out.println(e);}   }  
   }      
 }   public class TestSynchronization2{  
}   public static void main(String args[]){  
class MyThread1 extends Thread{   Table obj = new Table();//only one object  
Table t;   MyThread1 t1=new MyThread1(obj);  
MyThread1(Table t){   MyThread2 t2=new MyThread2(obj);  
this.t=t;   t1.start();  
}   t2.start();  
public void run(){   }  
t.printTable(5);   }  
}  
  
}  
Output:
5
10
15
20
25
100
200
300
400
500
INTER-THREAD
COMMUNICATION(COOPERATION)
 Cooperation (Inter-thread communication) is a mechanism
in which a thread is paused running in its critical section
and another thread is allowed to enter (or lock) in the
same critical section to be executed.It is implemented by
following methods of Object class:
 wait()
 notify()
 notifyAll()
1) wait() method
 Causes current thread to release the lock and wait until
either another thread invokes the notify() method or the
notifyAll() method for this object, or a specified amount
of time has elapsed.
 The current thread must own this object's monitor, so it
must be called from the synchronized method only
otherwise it will throw exception.

public final void wait()throws InterruptedException


2) notify() method

 Wakes up a single thread that is waiting on this object's


monitor. If any threads are waiting on this object, one of
them is chosen to be awakened. The choice is arbitrary
and occurs at the discretion of the implementation.

Syntax:
public final void notify()
3) notifyAll() method

 Wakes up all threads that are waiting on this object's


monitor.

Syntax:
public final void notifyAll()

You might also like