Java Puzzler

What do you expect this simple little programm to print out? Would it make any difference if this is a Groovy or a Java file? Please write your answer in the comments.

package betsy;
public class CodePuzzler {
public static void main(String[] args) {
// init array
String[] array = new String[2];
array[0] = "One";
array[1] = "Two";
// iterate and print in thread
for(final String element : array){
new Thread() {
public void run() {

view raw
hosted with ❤ by GitHub

3 thoughts on “Java Puzzler

  1. The Code should output either
    or (if the other thread gets to the println earlier)
    but I suspect that the first version is the one you will usually see (first time slice normally sufficient for first thread to complete its work).
    I do not know enough about Groovy to predict anything but I suspect that at least the code will compile without errors and the thread timing is also not really guaranteed – as usual.
    My Java 1.7.0_21-b11 performs as suspected above (although I admit, I put the class in the default package; the package should not make any difference, though).
    What’s the point? Did you get different results? Or is it just a Groovy-question?

  2. Hello Martin,

    you are correct for running this code in Java. In Groovy, however, it always prints out the following:


    Thus, in the Groovy version, both Threads reference the same String. This is caused by the way final is handled within a for-each loop in Groovy – the value is only set once and not per iteration.

    • Ouch! That’s strange…
      The for-each loop should bind “element” to each array element in turn and – in my opinion – raise a compile error if this is not possible (due to the “final” modifier).
      I would summarize it like so:
      A) this is not a “Java Puzzler” but a 100% Groovy thing.
      B) IMO Groovy has broken the old rule to not unnecessarily surprise people here: No sane programmer would have expected such behaviour: that’s not a “puzzler”, it’s a serious design flaw!

      And thanks for sharing this: now I know why I need not spend time on Groovy and can save my time for Clojure:
      (doseq [element [“One” “Two”]] (.start (Thread. #(println element))))
      prints the words sometimes in reverse order, but that should be possible.
      What surprised me, though, was one try where it printed both on the same line. I assume that println calls System.out.print() and appends the newline in another call without synchronizing the outputs such that both form one atomic operation. But since this was never promised and Clojure has enough constructs to let me deal with threading myself that’s ok for me: expecting the same behaviour for 2 different println functions was probably just my fault…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s