In simple words a static syncronized method will lock the class instead of the object, and it will lock the class because the keyword static means: "class instead of instance". The keyword syncronized means that only one thread can access the method at a time.
class Test { void show(int n) { for(int i=1;i<=3;i++) { System.out.println(n*i); try { Thread.sleep(500); } catch(Exception e) { } } } } class MyThread1 extends Thread { public void run() { Test.show(2); } } class MyThread2 extends Thread { public void run() { Test.show(3); } class MyThread3 extends Thread3 { public void run() { Test.show(4); } } class MyThread4 extends Thread { public void run() { Test.show(5); } } class StaticSynchronizeDemo { public static void main(String[] args) { MyThread1 ob1= new MyThread1(); MyThread2 ob2= new MyThread2(); MyThread3 ob3= new MyThread3(); MyThread4 ob4= new MyThread4(); ob1.start(); ob2.start(); ob3.start(); ob4.start(); } }
Output
2
4
6
3
6
9
4
8
12
5
10
15
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc