public class ThreadTest {
private int j;
public static void main(String args[]) {
ThreadTest tt = new ThreadTest();
Inc inc = tt.new Inc();
Dec dec = tt.new Dec();
Thread t = new Thread(inc);
t.start();
t = new Thread(dec);
t.start();
}
private synchronized void inc() {
System.out.println(Thread.currentThread().getName() + "-inc:" + ++j);
}
private synchronized void dec() {
System.out.println(Thread.currentThread().getName() + "-dec:" + --j);
}
class Inc implements Runnable {
public void run() {
inc();
}
}
class Dec implements Runnable {
public void run() {
dec();
}
}
}