Saturday, 10 June 2017

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


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:

CLICK HERE

No comments:

Post a Comment