您的位置:首页 > 其它

第九天 设计一个方法,执行一次,一个线程对j加1,另外一个对j减一

2017-05-18 00:32 253 查看
public class Q55_3 {
public static void main(String[] args) {
Q55_3.test t=new Q55_3().new test();
t.a();
}
int j=0;

public synchronized int incre(){
System.out.println(j);
return j++;

}
public  synchronized int desce(){
System.out.println(j);
return j--;

}
class test{
public void a(){
for(int i=0;i<2;i++){
new Thread(new Runnable(){
public void run(){incre();}
}).start();
new Thread(){
public void run(){desce();}
}.start();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  案例 线程 设计 总结
相关文章推荐