欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java,

来源: javaer 分享于  点击 49870 次 点评:97

java,


class Count{

private long count =0;

public long getCount(){
return count;
}
//thread safe fix 1000
public synchronized void service(){
++count;
System.out.println(Thread.currentThread().getName());
}
//thread not safe[998 or 997 my test result]
public void service1(){
++count;
System.out.println(Thread.currentThread().getName());
}
}
public class Thread2_2 {


public static void main(String[] args) throws InterruptedException {

final Count count = new Count();
for(int i=0;i<1000;i++){
new Thread(new Runnable(){
@Override
public void run() {
count.service();
}
}).start();
}
//wait all the threads finished(10 seconds is ok)
Thread.sleep(10000L);
System.out.println(count.getCount());
}
}

相关文章

    暂无相关文章
相关栏目:

用户点评