java中synchronized可见性问题
同步代码块不是说能保证可见性吗 怎么还线程0感知不到total=1
java中synchronized可见性问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 小飞LOVE霞 2023-01-30 14:52关注
package csdn20230129; /** * @Description * @Author wangFei * @Date 2023/1/30 10:15 * @Version 1.0 **/ public class Test { int total = 0; public static void main(String[] args) throws InterruptedException { Test test = new Test(); test.a(); test.b(); } public synchronized void a() { new Thread(() -> { System.out.println("a线程 " + Thread.currentThread().getName() + " 执行"); synchronized (this) { while (total == 0); System.out.println("a-1-end"); } }).start(); } public synchronized void b() { new Thread(() -> { System.out.println("b线程 " + Thread.currentThread().getName() + " 执行"); total = 1; System.out.println("b线程 " + Thread.currentThread().getName() + " 执行完毕,当前total=" + total); }).start(); } }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 mmo能不能做客户端怪物
- ¥15 osm下载到arcgis出错
- ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
- ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
- ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
- ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
- ¥15 QQ邮箱过期怎么恢复?
- ¥15 登录他人的vue项目显示服务器错误
- ¥15 (标签-android|关键词-app)
- ¥15 comsol仿真压阻传感器