Synchronized Multithreading Process

In our last blog, we saw asynchronized process. In this blog we will see how to synchronized multithreading process.

What is synchronized multithreading process?

All time multiple processes need prevent thread interference and prevent consistency problem. Using of object and method synchronized we can manage multithreading process in synchronized mode.

In this blog, we first see object synchronized and then we will see method synchronized.


Object Synchronized


Syn1 class in otherPack package : Display name in {}

package otherPack;

public class Syn1

{

            public void display(String name)

            {

                        try

                        {

                                    System.out.print(” { ” + name );

                                    Thread.sleep(1000);

                                    System.out.println(” } “);

                        }

                        catch(Exception ex){}

            }

}

 

SynNized1 class in classStore package : All process

package classStore;

import otherPack.*;

 

public class SynNized1 implements Runnable

{

            Thread t;

            Syn1 obj;

            String name;

            public SynNized1(String name,Syn1 obj)

            {

                        t = new Thread(this);

                        this.obj = obj;

                        this.name = name;

                        t.start();

            }

            public void run()

            {

                        Synchronized(obj)

                        {

                                    obj.display(name);

                        }

            }

}

 

Thread8 class in examples package

package examples;

import classStore.*;

import otherPack.*;

public class Thread8

{

                public static void main(String[] args)

                {

                                Syn1 obj = new Syn1();

SynNized1 ob1 = new SynNized1(“Dipak”,obj);

                                SynNized1 ob2 = new SynNized1(“Umang”,obj);

                                SynNized1 ob3 = new SynNized1(“Rohit”,obj);

                }

}


Method Synchronized


Syn2 class in otherPack package : Display name in {}

package otherPack;

public class Syn2

{

            synchronized public void display(String name)

            {

                        try

                        {

                                    System.out.print(” { ” + name );

                                    Thread.sleep(1000);

                                    System.out.println(” } “);

                        }

                        catch(Exception ex){}

            }

}

 

SynNized2 class in classStore package : All process

package classStore;

import otherPack.*;

 

public class SynNized2 implements Runnable

{

            Thread t;

            Syn2 obj;

            String name;

            public SynNized2(String name,Syn2 obj)

            {

                        t = new Thread(this);

                        this.obj = obj;

                        this.name = name;

                        t.start();

            }

            public void run()

            {

obj.display(name);

            }

}

 

Thread9 class in examples package

package examples;

import classStore.*;

import otherPack.*;

public class Thread9

{

                public static void main(String[] args)

                {

                                Syn2 obj = new Syn2();

SynNized2 ob1 = new SynNized2(“Dipak”,obj);

                                SynNized2 ob2 = new SynNized2(“Umang”,obj);

                                SynNized2 ob3 = new SynNized2(“Rohit”,obj);

                }

}


Output:

{ Dipak }

{ Umang }

{ Rohit }

 

Video also available on YouTube Java Program Channel


 

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

548total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

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

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image