首页 新闻 会员 周边

println(count++)为什么会出现数据不同步的情况

0
[待解决问题]

public class Test {

public static void main(String[] args) {
    Runnable runnable = new Runnable() {
        private int count = 0;
        @Override
        public void run() {
            try {
                for (int i = 0; i < 10; i++) {
                    System.out.println(count++);
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    Thread a = new Thread(runnable);
    a.start();
    Thread b = new Thread(runnable);
    b.start();
}

}
=========================console========================
0
1
2
3
4
5
6
6
8
7
9
9
10
10
11
12
13
14
15
16
===================================================
println方法是使用synchronized修饰的同步方法,为什么还会出现这种数据count不同步的情况?

*青锋*的主页 *青锋* | 初学一级 | 园豆:180
提问于:2018-10-17 16:46
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册