crossoverJie / crossoverJie/JCSprout

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

Open
#165 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
26.8k
Forks
7k
PR merge metrics
No merged PRs in 30d

Description

[代码地址](https://github.com/crossoverJie/JCSprout/blob/master/src/main/java/com/crossoverjie/actual/TwoThread.java)

**运行结果**:
![image](https://user-images.githubusercontent.com/30221890/62414111-587e0600-b64a-11e9-98f6-70f4b96039cb.png)

**分析**
1、当奇数线程在箭头处发生上下文切换
2、偶数线程获取锁,输出1000,并将number.start加为1001,释放锁,判断1001>1000,线程结束
3、奇数线程恢复执行,就会输出1001
![image](https://user-images.githubusercontent.com/30221890/62414214-967b2a00-b64a-11e9-8cda-9038125b1938.png)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with src/main/java/com/crossoverjie/actual/TwoThread.java and reproduce the alternating-thread output repeatedly. Inspect the synchronization around the number limit and verify that the program never prints a value above 1000; done means the observed race is eliminated without breaking alternating output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.