Below are 2 code snippets. doSomething() in first one is synchronized. doSomething() in second one is synchronized static.
For synchronized:
When calling doSomething() within 2 different instances at the same time, it can be concurrent.
When calling doSomething() within same instance at the same tim, it can’t be concurrent.
For synchronized static:
When calling doSomething() within any instances at the same time, it can’t be concurrent.
public class SynchronizedDefaultClass {
public static int a;
// with synchronized, this method could be called only by 1 thread.
public synchronized void doSomething() {
for (int i = 0; i < 5; i++) {
a++;
System.out.println(String.format("Thread: %s, value: %d", Thread.currentThread().getId(), a));
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}
public class SynchronizedStaticClass {
public static int a;
// with synchronized, this method could be called only by 1 thread.
public synchronized static void doSomething() {
for (int i = 0; i < 5; i++) {
a++;
System.out.println(String.format("Thread: %s, value: %d", Thread.currentThread().getId(), a));
try {
Thread.sleep(1000);
} catch (Exception e) {}
}
}
}
Caller1:
public class Caller1 implements Runnable {
SynchronizedDefaultClass synchronizedClass;
public Caller1(SynchronizedDefaultClass synchronizedClass) {
this.synchronizedClass = synchronizedClass;
}
public void run() {
synchronizedClass.doSomething();
}
/*
Call a synchronized method belonging to same instance.
*/
public static void main(String[] args) {
SynchronizedDefaultClass synchronizedClass = new SynchronizedDefaultClass();
Thread c1 = new Thread(new Caller1(synchronizedClass));
Thread c2 = new Thread(new Caller1(synchronizedClass));
c1.start();
c2.start();
}
}
Caller2:
public class Caller2 implements Runnable {
SynchronizedDefaultClass synchronizedClass;
public Caller2(SynchronizedDefaultClass synchronizedClass) {
this.synchronizedClass = synchronizedClass;
}
public void run() {
synchronizedClass.doSomething();
}
/*
Call a synchronized method belonging to different instances.
*/
public static void main(String[] args) {
SynchronizedDefaultClass synchronizedClass1 = new SynchronizedDefaultClass();
SynchronizedDefaultClass synchronizedClass2 = new SynchronizedDefaultClass();
Thread c1 = new Thread(new Caller2(synchronizedClass1));
Thread c2 = new Thread(new Caller2(synchronizedClass2));
c1.start();
c2.start();
}
}
Caller3:
public class Caller3 implements Runnable {
SynchronizedStaticClass synchronizedClass;
public Caller3(SynchronizedStaticClass synchronizedClass) {
this.synchronizedClass = synchronizedClass;
}
public void run() {
synchronizedClass.doSomething();
}
/*
Call a synchronized static method belonging to different instances.
*/
public static void main(String[] args) {
SynchronizedStaticClass synchronizedClass1 = new SynchronizedStaticClass();
SynchronizedStaticClass synchronizedClass2 = new SynchronizedStaticClass();
Thread c1 = new Thread(new Caller3(synchronizedClass1));
Thread c2 = new Thread(new Caller3(synchronizedClass2));
c1.start();
c2.start();
}
}
github