Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Using a single Runnable in multiple threads

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I just don't get it... In the following example, a single Runnable target is used in multiple threads (from page 707 of the SCJP Study Guide 310-065):



Since there is only 1 instance of MyRunnable, how can it be used as the target in multiple threads? Won't this cause the data in the run method to become corrupted?

Thanks,
realPigs
 
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Real Pigs wrote:Since there is only 1 instance of MyRunnable, how can it be used as the target in multiple threads?



That's kind of hard to answer, since using objects is normal behaviour in Java and it's perfectly normal to pass references to objects around without having to deal with arbitrary rules about who can get the references and who can't.

Won't this cause the data in the run method to become corrupted?



No, it couldn't. The data in the run method -- I will interpret that as if you said "the local variables in the run method" -- belong to the thread which is using the MyRunnable object. So if there are three threads using the object at the same time, each of them has their own copy of those local variables.

However if you had asked "Won't this cause the data in the MyRunnable object to become corrupted?" then the answer would be different. In this case there's only one MyInstance object, and hence only one copy of its instance variables. And yes, each of the three threads could be accessing those variables at the same time, and bad things could happen if those accesses happened in an uncontrolled way.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only if the Runnable is written in a way making that possible. If, for instance, MyRunnable has no member variables, and the run() method only uses local variables, then the multiple threads don't have to interact at all. You could write it in a way that this would be dangerous, but that's not a reason to prevent it from happening. As far as the compiler is concerned, all it's ever doing is passing an argument into a constructor, and why would it complain about that?

[Edit - that was a reply to the original question, not to Paul!]
reply
    Bookmark Topic Watch Topic
  • New Topic