Tuesday 28 October 2014

synchronization in java

Structure of the Problem Requirements 

In this Program we will learn how to implement multiple threads in java. Access through a single resources by multiple threads is called Synchronization. In this program we make a simple class of sync which is extending threads and there is one static string message on which we can apply synchronization. Here is the source code of this problem which help you in better understanding.

SOURCE CODE







classSync extends Thread
{
                static String msg[]={"Helo", "There", "is ", "AN", "Developer", "LEP"};

                Sync(String thread)

                {

                                                super(thread);

                }

                public void run()

                {

                                                Display(getName());

                }

                public synchronized voidDisplay(String thread)

                {

                                for(inti=0;i<=4;i++)

                                                                System.out.println(thread+msg[i]);

                                try

                                {

                                                                this.sleep(10000);

                                }

                                catch(Exception e)

                                {

                                }

                }

}

publicclasssynchronization 
{

                public static void main(String[] argss)

                {

                                                Sync S1=newSync("First Thread : ");

                                                S1.start();

                                                Sync S2=newSync("Second Thread : ");

                                                S2.start();
                                               
                }

}


OUTPUT OF THE PROGRAM


Synchronization in java
Synchronization in java


Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment