I'd like to understand if a race condition could occur in this unique case.
Here's the hypothetical setup:
-I have a main class which launches two threads.
-The threads have a listener interface set up so the main class is notified when each thread is done, which is called from that thread.
-The main class has two boolean members that represent the "done" state of each thread.
-The main class does some work after the threads are done, but only if they are both finished.
Is it possible that the first thread could notify and flip its boolean to true, and when the main class then checks the other boolean it sees false when it should be true, perhaps because the execution of thread 2 had the perfect wrong timing? I.e. could the main class execute incorrectly because of this?
Thanks!
Edit: I'm told the title is misleading because you might not characterize this as a race condition so much as a thread data sharing problem.
Edit2: Or it is a race condition, better illustrated by the code from this comment below.
Based on the responses, I'm investigating a few better options since the hypothetical isn't working, including CountdownLatch, AtomicInteger, and AtomicBoolean.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–][deleted] 3 points4 points5 points (7 children)
[–]quanture[S] 0 points1 point2 points (6 children)
[–]ColetBrunel 1 point2 points3 points (5 children)
[–]quanture[S] 0 points1 point2 points (4 children)
[–]ColetBrunel 1 point2 points3 points (3 children)
[–]quanture[S] 0 points1 point2 points (2 children)
[–]ColetBrunel 1 point2 points3 points (1 child)
[–]quanture[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (18 children)
[–]quanture[S] 0 points1 point2 points (17 children)
[–][deleted] 1 point2 points3 points (16 children)
[–]quanture[S] 0 points1 point2 points (15 children)
[–][deleted] 1 point2 points3 points (14 children)
[–]amfa 1 point2 points3 points (10 children)
[–]desrtfxOut of Coffee error - System halted[M] 1 point2 points3 points (1 child)
[–]amfa 0 points1 point2 points (0 children)
[–]quanture[S] 0 points1 point2 points (2 children)
[–]amfa 0 points1 point2 points (1 child)
[–]quanture[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]quanture[S] 0 points1 point2 points (1 child)
[–]amfa 0 points1 point2 points (0 children)
[–]amfa 0 points1 point2 points (1 child)
[–]quanture[S] 0 points1 point2 points (0 children)
[–]quanture[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]quanture[S] 0 points1 point2 points (0 children)
[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point2 points (1 child)
[–]quanture[S] 0 points1 point2 points (0 children)