Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

交替打印奇偶数,控制输出到1000,但是经过测试发现有时会输出到1001 #165

Open
chensongbin opened this issue Aug 3, 2019 · 2 comments

Comments

@chensongbin
Copy link

代码地址

运行结果
image

分析
1、当奇数线程在箭头处发生上下文切换
2、偶数线程获取锁,输出1000,并将number.start加为1001,释放锁,判断1001>1000,线程结束
3、奇数线程恢复执行,就会输出1001
image

@chensongbin
Copy link
Author

改进思路:
可以参考Double Check机制
image

@zongJianKun
Copy link

zongJianKun commented Aug 3, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants