Thursday, 16 February 2017

Java Tutorial: Java Synchronization (Static synchronization method[anonymous thread])


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

Click the below Image to Enlarge
Java Tutorial: Java Synchronization (Static synchronization method[asynchronous thread]) 
StaticSynchronizationDemo.java
/*
 * In this example, we are using anonymous class to create the threads.
 */

class Table
{

    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("------------------------");
    }
}

public class StaticSynchronizationDemo
{
    public static void main(String t[])
    {
        Thread t1 = new Thread()
        {
            public void run()
            {
                Table.printTable(1);
            }
        };

        Thread t2 = new Thread()
        {
            public void run()
            {
                Table.printTable(10);
            }
        };

        t1.start();
        t2.start();

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

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

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

    Google+ Followers

    Google+ Badge