package itat;
public class Example9_4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread threadA,threadB,threadC,threadD;
TargetObject a1=new TargetObject(),
a2=new TargetObject();
threadA=new Thread(a1);
threadB=new Thread(a1);
a1.setNumber(10);
threadA.setName("add");
threadB.setName("add");
threadC=new Thread(a2);
threadD=new Thread(a2);
a2.setNumber(-10);
threadC.setName("sub");
threadD.setName("sub");
threadA.start();
threadB.start();
threadC.start();
threadD.start();
}
}
class TargetObject implements Runnable{
private int number=0;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void run() {
// TODO Auto-generated method stub
while(true){
if(Thread.currentThread().getName().equals("add")){
number++;
System.out.println("现在number等于"+number);
}
if(Thread.currentThread().getName().equals("sub")){
number--;
System.out.println("现在number等于"+number);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}