Thursday, 16 February 2017

Java Tutorial: Java Synchronization (Static synchronization method | Synchronization in java)


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

Click the below Image to Enlarge
Java Tutorial: Java Synchronization (Static synchronization method | Synchronization in java) 
StaticSynchronizationDemo.java
class Table
{

    /*
     * We are applying synchronized keyword on the static
     * method to perform static synchronization.
     */
    synchronized static void printTable(int n)
    {
        System.out.println(Thread.currentThread().getName());
        for (int i = 1; i <= 5; i++)
        {
            System.out.println(n * i);
            try
            {
                Thread.sleep(400);
            }
            catch (Exception e)
            {
            }
        }
        System.out.println("------------------------");
    }
}

class MyThread1 extends Thread
{
    public void run()
    {
        Table.printTable(1);
    }
}

class MyThread2 extends Thread
{
    public void run()
    {
        Table.printTable(10);
    }
}

public class StaticSynchronizationDemo
{
    public static void main(String t[])
    {
        MyThread1 t1 = new MyThread1();
        MyThread2 t2 = new MyThread2();
        t1.start();
        t2.start();

    }
}
Output
Thread-1
10
20
30
40
50
------------------------
Thread-0
1
2
3
4
5
------------------------
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ThreadDemo_static_synchronization_method_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/d83d752fbc13c2d1cd6c6655b89d6e57fdda2f22/BasicJava/ThreadDemo_static_synchronization_method_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