While writing test code for a networking component I ran into the following issue. I created a thread to read from a socket, started it and then wrote to a client socket. After writing called thread.join() and expected it to finish executing my server socket thread before continuing. Instead what I get is non-deterministic behavior. Sometimes the thread finishes sometimes not. Since the thread isn't doing much if I add a sleep the code works (my work around in my test code). To be clear I'm not expecting that the print statements execute in any particular order - I'm concerned that the lines won't get added to the list and thus the current thread hasn't actually waited for the spawned thread to exit.
I've attached a sample groovy class that exhibits the problem and the equivalent Java code that does not have the issue.