This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
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?
 
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.
 
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!