Thread Synchronization



Thread Synchronization:

Execution of a thread is asynchronous by nature that is context switch between thread can not be predicted in advance.

 Synchronization is desirable when multiple threads share a common resource in order to use the resource mutually exclusive manner.

E.g. two users did not access the same bank account simultaneously.

“Who thread when execute is not predicated; only no. of thread is fixed.”

Synchronization is achieved in two ways:-

1.    Synchronized method

One or more methods of a class can be declared to be synchronized. When a thread calls an object’s synchronized method, the whole object is locked. This means that if another thread tries to call any synchronized method of the same object, the call will block until the lock is released (which happens when the original call finishes).

synchronized returnType methodName(if arg any){

//statements

}

2.    Synchronized block

There are cases where we need to synchronize a group of statements; we can do that using synchronized statement.

synchronized (object){

object.method();

}

Example:

Synchronized method Test

public class MyReader {

    BufferedReader b;

    public MyReader() {

        b = new BufferedReader(new InputStreamReader(System.in));

    }

    synchronized public String readData(String msg){

        String str;

        try{

            System.out.println(msg);

            str=b.readLine();

            return str;

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

        return null;

    }

}

public class NameThread extends Thread{

final MyReader r;

public NameThread(MyReader reader){

    r=reader;

}

    @Override

public void run(){

     String name=r.readData("Enter name");

    System.out.println("Hello,"+name);

  

}

} public class MailThread extends Thread{

final MyReader r;

public MailThread(MyReader reader){

    r=reader;

}

    @Override

public void run(){

     

    String mailId=r.readData("Enter mailId");

    System.out.println("your EmailId "+mailId);

 

}

}

public class ReaderTest {

    public static void main(String as[]) {

        MyReader m = new MyReader();

        NameThread nt = new NameThread(m);

        MailThread mt = new MailThread(m);

        nt.start();

        mt.start();

    }

}

Synchronized block Test

public class MyReader {

    BufferedReader b;

    public MyReader() {

        b = new BufferedReader(new InputStreamReader(System.in));

    }

    public String readData(String msg){

        String str;

        try{

            System.out.println(msg);

            str=b.readLine();

            return str;

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

        return null;

    }

}

public class NameThread extends Thread{

final MyReader r;

public NameThread(MyReader reader){

    r=reader;

}

    @Override

public void run(){

        synchronized(r){

            String name=r.readData("Enter name");

    System.out.println("Hello,"+name);

        }

  

}

}

public class MailThread extends Thread{

final MyReader r;

public MailThread(MyReader reader){

    r=reader;

}

    @Override

public void run(){

        synchronized(r){

    String mailId=r.readData("Enter mailId");

    System.out.println("your EmailId "+mailId);

        }

 

}

}

public class ReaderTest {

    public static void main(String as[]) {

        MyReader m = new MyReader();

        NameThread nt = new NameThread(m);

        MailThread mt = new MailThread(m);

        nt.start();

        mt.start();

    }

}Output:

Enter name

raj

Hello,raj

Enter mailId

abc

Your EmailId abc

If in above scenarios synchronized method/block is not use then output –

Enter name

Enter mailId

No comments:

Post a Comment