public class Test {
public static int A = 0;
public static Object object = new Object();
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new RunnableA());
Thread thread2 = new Thread(new RunnableA());
Thread thread3 = new Thread(new RunnableB());
Thread thread4 = new Thread(new RunnableB());
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class RunnableA implements Runnable {
@Override
public void run() {
synchronized (Test.object) {
Test.A++;
}
}
}
class RunnableB implements Runnable {
@Override
public void run() {
synchronized (Test.object) {
Test.A--;
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
Data data = new Data();
Thread thread1 = new Thread(new RunnableA(data));
Thread thread2 = new Thread(new RunnableA(data));
Thread thread3 = new Thread(new RunnableB(data));
Thread thread4 = new Thread(new RunnableB(data));
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class Data {
private int A = 0;
public synchronized void increase() {
A++;
}
public synchronized void decrease() {
A--;
}
public int getA() {
return A;
}
}
class RunnableA implements Runnable {
private Data data;
public RunnableA(Data data) {
this.data = data;
}
@Override
public void run() {
data.increase();
}
}
class RunnableB implements Runnable {
private Data data;
public RunnableB(Data data) {
this.data = data;
}
@Override
public void run() {
data.decrease();
}
}