public class 线程的加加减减 {
a a1 = new a();
public static void main(String[] args) {
new 线程的加加减减().call();
}
public void call(){
for(int i=0;i<20;i++){
new Thread(new Runnable(){public void run(){ a1.jia();}}).start(); //第一种
new Thread(
new Runnable(){
public void run(){
a1.jian();
}
}).start(); //第二种
}
}
}
class a {
private int i=0;
public synchronized void jia(){ //同步
i++;
System.out.print(i);
}
public synchronized void jian(){
i--;
System.out.print(i);
}
}
Thread-线程的写法
最新推荐文章于 2024-07-16 02:25:25 发布