***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***

posts

    Thread Synchronization

    If two or more threads need to access a shared resource there should be some way that the resource will be used only by one thread at a time. If a thread is writing some data and another thread may be reading the same data at that same time may cause inconsistency. This can be overcome by synchronization.

    To implement the synchronous behavior java has synchronous method. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. All the other threads then wait until the first thread come out of the synchronized block.

    Synchronization in Java is the capability to control the access of multiple threads to any shared resource.

    Synchronization is built around an internal entity known as the lock or monitor. Every object has a lock associated with it. By convention, a thread that needs consistent access to an object's fields has to acquire the object's lock before accessing them, and then release the lock when it's done with them.

    There are two types of thread synchronization:

    mutual exclusive and inter-thread communication. 

    Mutual Exclusive

    Mutual Exclusive keeps threads from interfering with one another while sharing data. It can be achieved by following three ways:

    1. By Using Synchronized Method
    2. By Using Synchronized Block
    3. By Using Static Synchronization

    Java Synchronized Method

    If any method is declared 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.

    //example of java synchronized method 

    class Table{ 

     synchronized void printTable(int n){     //synchronized method 

       for(int i=1;i<=5;i++){ 

         System.out.println(n*i); 

         try{ 

          Thread.sleep(400); 

         }catch(Exception e){System.out.println(e);} 

       } 

     } 

    } 

    class MyThread1 extends Thread{ 

    Table t; 

    MyThread1(Table t){ 

    this.t=t; 

    } 

    public void run(){ 

    t.printTable(5); 

    } 

    } 

    class MyThread2 extends Thread{ 

    Table t; 

    MyThread2(Table t){ 

    this.t=t; 

    } 

    public void run(){ 

    t.printTable(100); 

    } 

    } 

    public class TestSynchronization2{ 

    public static void main(String args[]){ 

    Table obj = new Table();//only one object 

    MyThread1 t1=new MyThread1(obj); 

    MyThread2 t2=new MyThread2(obj); 

    t1.start(); 

    t2.start(); 

    } 

    } 

    Synchronized Block in Java

    Synchronized block can be used to perform synchronization on any specific resource of the method. Scope of synchronized block is smaller than the method. Java synchronized block is more efficient than Java synchronized method.

    Syntax:

    synchronized (object reference expression) {    

      //code block    

    }  

    Static Synchronization

    If any static method is synchronized, the lock will be on the class not on object. 

    Suppose there are two objects of a shared class named object1 and object2. In case of synchronized method and synchronized block there cannot be interference between t1 and t2 or t3 and t4 because t1 and t2 both refers to a common object that have a single lock. But there can be interference between t1 and t3 or t2 and t4 because t1 acquires lock and t3 acquires another lock. Static synchronization is used if no interference between t1 and t3 or t2 and t4 has to be present.


    Inter-thread Communication

    Java threads can communicate with each other using methods are wait(), notify(), notifyAll().

    These methods can only be called from within a synchronized method.


    1) To understand synchronization java has a concept of monitor. Monitor can be thought of as a box which can hold only one thread. Once a thread enters the monitor all the other threads have to wait until that thread exits the monitor.


    2) wait()  tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify().The 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.

     

    Method

    Description

    public final void wait()throws InterruptedException

    It waits until object is notified.

    public final void wait(long timeout)throws InterruptedException

    It waits for the specified amount of time.


    3) notify() wakes up the first thread that called wait() on the same object. notifyAll() wakes up all the threads that called wait() on the same object. The highest priority thread will run first.


    public final void notify()  

                public final void notifyAll()  

    No comments:

    Post a Comment