I observed a problem, sometimes multi player game stuck, in my case I used 6 players and every time stuck after player4. Give me please some ticks about how to debug it ?
Raúl is a software engineer with a strong focus on code quality and readability. He likes to work following XP and Clean Code practices, and his favorite language is Java.
Thanks for your message, you have discovered a bug in my implementation, which reveals how hard it is to work with concurrent systems.
The problem is in myTurn.awaitUninterruptibly(), as its name expresses, this method is not interruptible, so when the main class interrupts all the threads, if one of the threads is blocked due to the call to this awaitUninterruptibly method, then the thread won't wake up. This situation is not extremely likely, in fact it didn't happen to me when I implemented the example, but I was able to reproduce it after reading your message.
Debugging this kind of issues is quite hard, and it took me some time to understand what was going on. The only solution that comes to my mind at the moment is changing the call to awaitUninterruptibly to await, which receives a timeout. We can configure a long timeout if we want, but for this case 1 minute is more than enough, in fact the timeout will never happen, as the method will be interrupted:
It's a great tutorial, thank you.
I observed a problem, sometimes multi player game stuck, in my case I used 6 players and every time stuck after player4. Give me please some ticks about how to debug it ?
Output:
Thanks for your message, you have discovered a bug in my implementation, which reveals how hard it is to work with concurrent systems.
The problem is in
myTurn.awaitUninterruptibly()
, as its name expresses, this method is not interruptible, so when the main class interrupts all the threads, if one of the threads is blocked due to the call to thisawaitUninterruptibly
method, then the thread won't wake up. This situation is not extremely likely, in fact it didn't happen to me when I implemented the example, but I was able to reproduce it after reading your message.Debugging this kind of issues is quite hard, and it took me some time to understand what was going on. The only solution that comes to my mind at the moment is changing the call to
awaitUninterruptibly
toawait
, which receives a timeout. We can configure a long timeout if we want, but for this case 1 minute is more than enough, in fact the timeout will never happen, as the method will be interrupted:Note that we need to catch
InterruptedException
and propagate the interruption to the thread, so the loop exits normally.I hope it makes sense, and again, thanks for your message!!!