Synchronized function
package com.pivincii;
public class AsynDemo {
synchronized public void countTo(int value) {
int count = 0;
while (count < value) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
System.out.println(Thread.currentThread() + " counted " + count);
}
}
public static void main(String[] args) {
AsynDemo asynDemo = new AsynDemo();
new Thread(() -> asynDemo.countTo(3)).start();
new Thread(() -> asynDemo.countTo(3)).start();
}
}