Thursday, 9 February 2017

Java Tutorial: Java Synchronization (Synchronization in java | synchronized block in java_V4)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=4ML7ZR3r_bU&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Java Synchronization (Synchronization in java | synchronized block in java_V4) 
Java Tutorial: Java Synchronization (Synchronization in java | synchronized block in java_V4) 
DisplayCounter.java
public class DisplayCounter
{

    public void printCount()
    {
        /*
         * synchronized block.
         */
        //synchronized (this)
        //{
            try
            {
                System.out.println("Thread Name = " + Thread.currentThread().getName());
                for (int i = 5; i > 0; i--)
                {
                    System.out.println("Counter   ---   " + i);
                    Thread.sleep(200);
                }
                System.out.println();               
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        //}

    }
}
DisplayThread.java
public class DisplayThread extends Thread
{
    private DisplayCounter dc;

    public DisplayThread(String name, DisplayCounter dc)
    {
        super.setName(name);
        this.dc = dc;
    }

    public void run()
    {
        dc.printCount();
    }

}
SynchronizationDemo.java
public class SynchronizationDemo
{
    public static void main(String[] args)
    {
        DisplayCounter displayCounter = new DisplayCounter();

        DisplayThread thread1 = new DisplayThread("Thread1", displayCounter);
        DisplayThread thread2 = new DisplayThread("Thread2", displayCounter);

        thread1.start();
        thread2.start();
    }
}
Output
-------------------------------------
If printCount method has synchronized block,
then below will be the output:
-------------------------------------

Thread Name = Thread1
Counter   ---   5
Counter   ---   4
Counter   ---   3
Counter   ---   2
Counter   ---   1

Thread Name = Thread2
Counter   ---   5
Counter   ---   4
Counter   ---   3
Counter   ---   2
Counter   ---   1

-----------------------------------------
If printCount method does not have synchronized block,
then below will be the output:
-----------------------------------------
Thread Name = Thread1
Thread Name = Thread2
Counter   ---   5
Counter   ---   5
Counter   ---   4
Counter   ---   4
Counter   ---   3
Counter   ---   3
Counter   ---   2
Counter   ---   2
Counter   ---   1
Counter   ---   1
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/Synchronization_Block_Demo_V4_File_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/Synchronization_Block_Demo_V4_File_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/644abf74fceeb240066dd3d2fe2e4b1fb6c4bd48/BasicJava/Synchronization_Block_Demo_V4_File_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment