Click here to watch in Youtube :
https://www.youtube.com/watch?v=vexva-R2xOo&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
![]() |
Java Tutorial: Java Synchronization (Static synchronization block | Synchronization in java) |
class Table
{
static void printTable(int n)
{
/*
* The block synchronizes on the lock of the object
* denoted by the reference .class name .class
*/
synchronized (Table.class)
{
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();
}
}
OutputThread-0
1
2
3
4
5
------------------------
Thread-1
10
20
30
40
50
------------------------
Click the below link to download the code:
No comments:
Post a Comment