-
Notifications
You must be signed in to change notification settings - Fork 0
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
2장 RxJava를 사용하는 데 필요한 배경 지식 #5
Comments
Open
2-13의 예제에서 동시성 문제가 생길 수 있다고 돼있는데 어떻게 해야 저 경우를 재현할 수 있을까? class Point {
private val x = AtomicInteger(0)
private val y = AtomicInteger(0)
fun rightUp() {
repeat(1000) {
x.incrementAndGet()
}
repeat(1000) {
y.incrementAndGet()
}
}
fun getX() = x.get()
fun getY() = y.get()
}
라고 책에는 적혀져 있는데 이렇게 해도 결국에는 동일한 값을 출력하게 되던데 어떻게 해야 할까 🤔 fun main() {
val point = Point()
val task = Runnable {
for (i in 0..9999) {
point.rightUp()
}
}
val executorService = Executors.newCachedThreadPool()
val future1 = executorService.submit(task, true)
val future2 = executorService.submit(task, true)
val future3 = executorService.submit(task, true)
val future4 = executorService.submit(task, true)
val future5 = executorService.submit(task, true)
val future6 = executorService.submit(task, true)
if (future1.get() && future2.get() && future3.get() && future4.get() && future5.get() && future6.get()) {
println("${point.getX()},${point.getY()}")
} else {
println("실패")
}
executorService.shutdown()
} |
Open
혹시 몰라 자바로도 테스트 해봤는데 20000, 20000 추가로 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: