• Post Reply Bookmark Topic Watch Topic
  • New Topic

can we access an instace variable of a thread which is in wait() mode?  RSS feed

 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am having a class R implementing runnabale interface. it has an instance variable A with its getter and setter methods.
i am creating a thread T using interface R. In the run() method of R i am creating a different thread T1 using different Runnable interface R1
Thread T will be in wait mode until thread t1 has completed. In run() method of R1 can i access the instance variable A using its getter method (provided R1 having R reference)

Can a thread access the instance variable of other thread which is in wait() mode?
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it yourself?

You took the time to post the question, but did you run it with real values and see what happened?

Had you done that, then you wouldn't have posted this question.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I tried it, but i want to make sure and understand the logic behind it.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance variables of a class belong to the instance of that class, not any particular thread. So you can access them from any thread, which thread created it is irrelevent. Where this becomes murky is when you're trying to keep state and data consistent across different threads which may be executing methods concurrently and may even have their own copies of a variable.

Originally posted by Edward Harned:
Did you try it yourself?

You took the time to post the question, but did you run it with real values and see what happened?

Had you done that, then you wouldn't have posted this question.


That's a bit harsh, particularly when it comes to multithreading. Observing results does not directly translate to accurately understanding what caused the observations. After all, would you conclude that double-checked locking is safe if you observed it to be so, or would you go seek some other authority to confirm that it actually is safe and you just didn't happen to encounter the problem yet?
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harsh?

To answer his question all he has to do is run it and print the result or use a debugger.

His question was simply:
Can a thread access the instance variable of other thread which is in wait() mode?

Run it and find out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!