Monitor object and synchronization of thread

When two or more than two threads are accessing common object then such object is known as monitor object or mutex (mutually exclusive object), monitor object always   shares common data in between two or more than two threads.

Let us consider two threads are sharing the common object, first thread is doing some processing on monitor object and second thread is displaying the data of monitor object. If during the execution of first thread, second thread interrupts the first thread then first will not able to complete its processing on monitor object and second thread will get incomplete data from the monitor object.

To solve the above problem, synchronized keyword is used as given in the following example.

Whenever any method is declared as synchronized then that method is known as synchronized method. During the execution of synchronized method, execution control will not allow other waiting threads to enter into process state but after the complete execution of synchronized method, it will allow waiting threads to enter into process state.

class Message

{

            public void show(String msg)

            {

                        System.out.println(“Hello “);

try

                        {

                        Thread.sleep(1000);

                        }

                        catch(Exception e)

                        {

                        System.out.println(e);

                        }

                        System.out.print(msg);

            }

}

class NewThread extends Thread

{

            private Message m;

            public NewThread(String name,Message m)

            {

                        super(name);

                        this.m=m;

                        start();

            }

            public void run()

            {

                        m.show(getName());

            }

}

class MyThead

{

            public static void main(String args[])

            {

                        Message m=new Message();

                        NewThread N1=new NewThread(“Babli”,m);

                        NewThread N2=new NewThread(“Bunty”,m);

                        try

                        {

                        N1.join();

                        N2.join();

                        }

                        catch(Exception e)

                        {

                        System.out.println(e);

                        }

            }

}

output:

            Hello Hello Babli

            Bunty

                                    In the above example, two threads are created. During the execution of main program, execution control will execute the process of first thread (babli thread) but due to sleep time it will be interrupted by second thread. To stop the interruption of other waiting threads we have to declare show() method of Message class as synchronized as given below.

class Message

{

            synchronized public void show(String msg)

            {

                        System.out.println(“Hello “);

try

                        {

                        Thread.sleep(1000);

                        }

                        catch(Exception e)

                        {

                        System.out.println(e);

                        }

                        System.out.print(msg);

            }

}

class NewThread extends Thread

{

            private Message m;

            public NewThread(String name,Message m)

            {

                        super(name);

                        this.m=m;

                        start();

            }

            public void run()

            {

                        m.show(getName());

            }

}

class MyThead

{

            public static void main(String args[])

            {

                        Message m=new Message();

                        NewThread N1=new NewThread(“Babli”,m);

                        NewThread N2=new NewThread(“Bunty”,m);

                        try

                        {

                        N1.join();

                        N2.join();

                        }

                        catch(Exception e)

                        {

                        System.out.println(e);

                        }

            }

}

output:

            Hello Babli

            Hello Bunty

Leave a Reply

Your email address will not be published. Required fields are marked *