MultiThreadShareData3 x = new MultiThreadShareData3();
Thread1 thread1 = x.new Thread1();
Thread2 thread2 = x.new Thread2();
for (int i = 0; i < 2; i++) {
new Thread(thread1).start();
new Thread(thread2).start();
}
}
class Thread1 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
// decrease();
ai.addAndGet(-1);
System.out.println(Thread.currentThread().getName()
+ " is changing data by decrementAndGet() "
+ ai.get());
}
}
}
class Thread2 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
// increase();
ai.addAndGet(1);
System.out.println(Thread.currentThread().getName()
+ " is changing data by incrementAndGet() "
+ ai.get());
}
}
}
public synchronized void decrease() {
System.out.println(Thread.currentThread().getName()
+ " decrease() is changing " + (--data));
}
public synchronized void increase() {
System.out.println(Thread.currentThread().getName()
+ " increase() is changing " + (++data));