java同步 静态变量的变化

wm124314 wm124314 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public class abc implements Runnable{
//共享资源变量
static int count = 0;

@Override
public synchronized void run() {
for (int i = 0; i < 2; i++) {
count=count+1;
System.out.println(Thread.currentThread().getName()+":"+count);
}
}

public static void main(String[] args) throws InterruptedException {
abc syncTest1 = new abc();
abc syncTest2 = new abc();
Thread thread1 = new Thread(syncTest1,"thread1");
Thread thread2 = new Thread(syncTest2, "thread2");
thread1.start();
thread2.start();
}
}
某次结果:
thread1:1
thread2:2
thread2:4
thread1:3

无法理解最后两行
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
wm124314
Java SE 62,621 成员 307,251 内容