• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I confirm 2 threads use the same variable?

 
Jim Venolia
Ranch Hand
Posts: 248
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got 2 threads that share a boolean to sync themselves. The debugger says the variable is false. One thread thinks it's false. The other thread acts like it's true.

The variable is "volatile boolean readyToDisplay;". Thread 2 runs off a swing timer, it begins life with

This works as expected. The main thread has this in it:

Here, it acts like readyToDisplay is true.

The only thing I can think of now is that somehow my 2 threads are using different copies of readyToDisplay. In C I'd use & to print the address of readyToDisplay at both places above to ensure they're using the same chunk o' memory. What's the Java equivalent of that?

Also, in C I can see the assembly code and registers and such, so I could see exactly why my while loop hangs. Is it possible to do something like this in Java?

Perhaps in Java volatile does not mean what I think it means

 
E Armitage
Rancher
Posts: 989
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use wait() and notify() methods instead of
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:Perhaps in Java volatile does not mean what I think it means

No, if the variable is volatile then changes to it are visible in all threads - as long as you are running in a JVM > 1.4.

Your problem is probably that the variable is not shared between threads, but you need to read your code to figure that out (we don't have the code so it is impossible for us to say).

But, like E Armitage said, the tight while() loop is a real bad idea - empty consumption of CPU. Use a blocking mechanism as he mentions - but again you need to make sure the Object you wait/notify on is the same object. The object itself can be arbitrary (Object syncOnMe = new Object()) but needs to be the same instance. Read the concurrency Java tutorials for help on how to do it.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.s. E Armitage: have a cow for giving the correct solution, answering the un-written What rather than the How.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are both threads accessing the same readyToDisplay? Can you show more of the code?

Note that in Java if you have a data member, the data member belongs to class that it belongs to. So, if you have 2 instances of the enclosing class, you have 2 separate instances of the enclosed data member. Changing one will not change the other.
 
Jim Venolia
Ranch Hand
Posts: 248
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Are both threads accessing the same readyToDisplay?

99% sure, but that's why I want to get at the address of the variable. To print it out and ensure both addresses are the same.

Won't get back to this til tomorrow or Wed, but I'll look into wait/notify.

Would like to point out my actual code had a sleep(20); in the while loop body, I wasn't just spinning waiting for a change.
 
Paul Clapham
Sheriff
Posts: 21875
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:99% sure, but that's why I want to get at the address of the variable. To print it out and ensure both addresses are the same.


But you can't get at the address of a variable in Java. You're going to have to look at the source code and analyze it.

Would like to point out my actual code had a sleep(20); in the while loop body, I wasn't just spinning waiting for a change.


It's still better to set up a notification mechanism so that you don't have to have a busy loop which does nothing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!