• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread variables, local or shared

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new at Java but thought that this would be the best place to ask my question.
My app will run some threads and I'm testing one thread class now. I'm now confused about which of the class variables are local and which are shared among instances of the thread.

In the below heuristic snippet I thought that "secondVariable" would be local but not the others and came across some information that none is local to a particular instance of the thread.
Which is correct? My code only uses private. If secondVariable is not local to the thread instance I'll need to protect it somehow .

Thakns, Nate
 
Ajezi Sajirava
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If more than one Thread can change any of the variables, you should synchronise the methods that do the changing, then they will be Thread-safe.
 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, firstVariable and secondVariable both belong to an instance of the Foo class. So if there are two instances of the class then each of them has their own copy of those variables. This has nothing to do with threads per se; it's possible for two threads to work with a single object. Also, private and public have nothing to do with threading. They simply control whether code in other classes can access those variables or not.

And thirdVariable is a static variable, so it belongs to the Foo class. So there is only one copy of that variable. This also has nothing to do with threads.

When you say
If secondVariable is not local to the thread instance I'll need to protect it somehow.
exactly what kind of "protection" did you have in mind? It's true that incrementing a variable is not an atomic operation; is that what you were talking about?
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exactly what kind of "protection" did you have in mind? It's true that incrementing a variable is not an atomic operation; is that what you were talking about?

No other classes either read or write to the variables I have so I don't think an atomic operation is relavant but I need to know that the class variables that I made "private" illustrated by "secondVariable" can't be read or written to by another instance of the class. The way I do this is to create two instances of the class Foo and then start a thread from each. I'm not at work right now so don't have the code in front of me. Each thread controls a networked "camera" with its own ID and image file-path-name string. The file name changes with each image of course. I'm trying to make sure that the threads work as I expect them to and work out communications with the cameras using the vendor's SDK/API.

Perhaps this is a class question and not a thread question?

In this case I don't want to share the private variables among class invocations. In any case I'll be creating more threads so need to be clear on who can access the private class variables.
 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all "private" means "only code in this class can access this variable". That means that any object in class Foo can access any instance variable in any other object of class Foo.

Your code doesn't have any examples of that. It only accesses its own instance variables. However you've already mentioned that the code you posted isn't the real code you're asking about, so I can't comment about your real code.


 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I understand.

How can state variables be made available to the thread that are unique to that particular thread? That do not share memory with other instances of the thread. For instance each thread needs to know the name of the camera from which it will receive data as well as the camera's handle and the camera's buffer.

I had thought that by creating two instances of the runnable class controlling the threads (only one thread started per class) that I could do that by passing the variables to the classes in the class initialization but from the discussion that doen't appear to be the case.

My place of business closed at noon today for the weekend but I could go in Friday to get code snippets. But then I'll be tempted to get lunch and a beer at the barbecue place.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!