public class MyThread2{
int j = 10;
public static void main(String[]args){
MyThread2 myt = new MyThread2();
for(int k=0;k<2;k++){
Inc inc = new Inc(myt);
inc.run();
Dec dec = new Dec(myt);
dec.run();
}
}
synchronized void dec(){
j--;
System.out.println("j="+j);
}
synchronized void inc(){
j++;
System.out.println("i="+j);
}
}
class Inc implements Runnable{
MyThread2 my;
Inc(MyThread2 myt){
my = myt;
}
@Override
public void run() {
my.inc();
}
}
class Dec implements Runnable{
MyThread2 my;
Dec(MyThread2 myt){
my = myt;
}
@Override
public void run() {
my.dec();
}
}