public class Topic9_1 {
public static void main(String rags[]){
Info info=new Info();
Add add=new Add(info);
Sub sub =new Sub(info);
Thread t1 = new Thread(add);
Thread t2 = new Thread(add);
Thread t3 = new Thread(sub);
Thread t4 = new Thread(sub);
t1.setName("线程1");
t2.setName("线程2");
t3.setName("线程3");
t4.setName("线程4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Info{
private int sum =0;
public synchronized void add(){
try {
Thread.sleep(300);
} catch (Exception e) {
e.printStackTrace();
}
sum++;
System.out.println(Thread.currentThread().getName()+"\t"+"+:"+sum);
}