• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

threads and access to variables

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's assume that Class1 has the following declaration
>public String foo = new String();
...And that Class1 starts Class2 as a new Thread
>Class2 c2 = new Class2;
>Class2.start();
...Does Class2 have access to foo declared in Class1??
Class1.foo = "bar";
Thanks,
Vernon
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vernon Gibson:
Let's assume that Class1 has the following declaration
>public String foo = new String();
...And that Class1 starts Class2 as a new Thread
>Class2 c2 = new Class2;
>Class2.start();
...Does Class2 have access to foo declared in Class1??
Class1.foo = "bar";


Yes, but not with this syntax (unless foo is static, that is). There is no isolation between threads in their access to each other's objects. In fact, a thread does not "own" any objects.
Actually, that's not entirely true -- but that is in most cases an academic point. On multiprocessor systems, you don't have any guarantee whatsoever that thread A can see modifications made by thread B until both have synchronized on the same object. But that is what you should do when threads share their data anyway.
- Peter
 
Vernon Gibson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clearing that up Peter.
I'm going to implement this in a situation where the creating class will hold statistics that will be updated by each thread as it is created and destroyed (i.e. how many active threads).
Thanks again
Vernon
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic