J类
package thread4;
public class J {
private int j;
public synchronized int getJ() {
return j;
}
public synchronized void AddJ() {
this.j ++;
}
public synchronized void DecJ() {
this.j --;
}
}
加线程类
package thread4;
public class Add extends Thread{
private J j;
public Add(J j) {
super();
this.j = j;
}
@Override
public void run() {
for(int i=0;i<100;i++) {
j.AddJ();
System.out.println(Thread.currentThread().getName()+"将j+1后,j="+j.getJ());
}
}
}
减线程类
package thread4