class MyClass {
...
public synchronized static someMethod() {
...
}
...
}
It is the equivalent to the following static synchronized block:synchronized ( MyClass.class ) {
...
}
Example of static synchronizationclass Account{
synchronized static void showAccount(String accountName){
System.out.println("My account name is "+accountName+" Holder Name is "+Thread.currentThread().getName());
try{
Thread.sleep(500);
}catch(Exception e){}
}
}
class MyThread1 extends Thread{
public void run(){
Account.showAccount("Dineshonjava.com");
}
}
class MyThread2 extends Thread{
public void run(){
Account.showAccount("Linkedin.com");
}
}
class MyThread3 extends Thread{
public void run(){
Account.showAccount("Facebook.com");
}
}
class MyThread4 extends Thread{
public void run(){
Account.showAccount("Twitter.com");
}
}
class StaticSyncDemo{
public static void main(String t[]){
MyThread1 t1 = new MyThread1();
MyThread2 t2 = new MyThread2();
MyThread3 t3 = new MyThread3();
MyThread4 t4 = new MyThread4();
t1.setName("DAV JavaServices");
t2.setName("dinesh.rajput");
t3.setName("dineshonjava");
t4.setName("admin@dineshonjava.com");
t1.start();
t2.start();
t3.start();
t4.start();
}
}

static void showAccount(String accountName) {
synchronized (Account.class) { // Synchronized block on class A
// ...
}
}
Labels: multithreading