0

I have this code

while(!decks.get(0).isEmpty()){
   SingleCardWindow obj = new SingleCardWindow(decks.get(0).take());
   while(obj.isVisible()){

   }
   System.out.println("Closed");
}

SingleCardWindow it is inherit-class from JFrame. This code displays all cards from the deck. Displays one card, wait until I close this window, and displays next card. In Windows it works well.

In linux (java-oracle-7) "Closed" never writing!

But if I make

while(!decks.get(0).isEmpty()){
   SingleCardWindow obj = new SingleCardWindow(decks.get(0).take());
   while(obj.isVisible()){
      System.out.println("SOMETHING");
   }
   System.out.println("Closed");
}

program works right. So, i think that compiler optimize "while(obj.isVisible())" like "while(true)". What I should do with this? I dont needs any code into the loop.

6
  • 1
    Sounds like obj.isVisible() is reading from non-volatile members, without explicit synchronization. Commented Aug 3, 2014 at 19:27
  • 1
    And of course, a tight loop like this is a really bad idea in general... Commented Aug 3, 2014 at 19:28
  • Note that even if you make this work, you never ever should. Busy-waiting is terribly inefficient. Commented Aug 3, 2014 at 19:28
  • Polling is never a good idea. There are events: use a listener and react to them. Commented Aug 3, 2014 at 19:30
  • 1
    Why do you believe this is a compiler optimization problem? Commented Aug 3, 2014 at 19:35

1 Answer 1

4

In the second case you added this line:

System.out.println("SOMETHING");

PrintStream#println is a synchronized method, so if that helped your program to start working, it is a quite clear indication that the method obj.isVisible is not synchronized, and since you don't use any explicit synchronization, you are effectively causing a data race in your program. The reading thread never observes the change in the value of the isVisible property.

But, as many others have already noticed, this just answers the question as you have asked it; the code you have posted has many more issues with an inappropriate programming model (busy waiting) used to solve an essentially event-based problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but I am really does not understand why is "data race". I have 2 threads, this is "Main thread", and "JFrame thread". JFrame thread can change "visible" property in the JFrame object. I close it - visible = false. JFrame thread is closed, so when Main thread wake - it reads visible False. We always has visible=true, and than one time - visible=false, we dont swap value between false and true, so main thread will read right value
@chubakur if a value isn't volitile/syncronised there is no guarantee that a change made on one thread is visible on another. They both are looking at their own local copy (this is done because it's wildly more efficient and you can use volitile/syncronised when you need interthread communication

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.