wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threading" Watch "Threading" New topic
Author

Threading

Tim Holmes
Greenhorn

Joined: Aug 26, 2005
Posts: 25
My question is in the below code, does each thread have it's own copy of someVariable? So if Thread "A" modifies "someVariable" and then thread "B" does also, then it doesn't really matter because they have their own copy right?


marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tim Holmes:
My question is in the below code, does each thread have it's own copy of someVariable? So if Thread "A" modifies "someVariable" and then thread "B" does also, then it doesn't really matter because they have their own copy right? ...

Yes, someVariable is an instance variable, so each instance of MyThread has its own "someVariable."

Whether this "matters" depends on whether it makes sense for each instance to have its own, or whether this should be a "shared" resource.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tim Holmes
Greenhorn

Joined: Aug 26, 2005
Posts: 25
This may seem like a dumb question, but i am trying to understand threading better. If that is the case that "someVariable" is unique to each thread and also that methods are unique to each thread, then what is the point in synchronizing? The whole point of synchronizing is so that a variable or method does not get accessed by more than one thread at any point in time, right? So the only time that would happen would be when a method or variable is "static" at which point there would be only one copy to work with and you would need to restrict it to one thread at a time. Right??
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Originally posted by Tim Holmes:
This may seem like a dumb question, but i am trying to understand threading better. If that is the case that "someVariable" is unique to each thread and also that methods are unique to each thread, then what is the point in synchronizing?


Correct, "local" variables do not need any "synchronizing".


The whole point of synchronizing is so that a variable or method does not get accessed by more than one thread at any point in time, right?

If you create in single instance of an object, lets say "Car", with the method "changeToNextGear()"
Then you create multiple threads calling the method changeToNextGear().
new Thread().start()->run.. => changeGear()
new Thread().start()->run.. => changeGear()
new Thread().start()->run.. => changeGear()

The thread 1
- reads gear=0
- set gear+1 = 1
- activate gear=1.

The thread 2:
- reads gear=1
- set gear+1 = 2

//In parallel, Thread 3 now becomes the active thread and execute the method
The thread 3:
- reads gear=2 (remember, thread2 just changed it)
- set gear+1=3
- activate gear=3 // your car just went from 1st to 3rd


So the only time that would happen would be when a method or variable is "static" at which point there would be only one copy to work with and you would need to restrict it to one thread at a time. Right??[/qb]


Anything else than local variables. Actually, not just variables, if you need to perform actions on a resource (a file), even if you are using local variable, you need to be aware that at anytime between 2 lines of code, another thread could be changing things.

Regards,
Alex
[ January 23, 2008: Message edited by: Alex Belisle Turcot ]
Tim Holmes
Greenhorn

Joined: Aug 26, 2005
Posts: 25
Ok, so I see now how you could use synchronize on a object so that only one thread accesses taht object at a time. That makes sense to me because you pass only one instance of a "runnable" object into the thread. Is it possible to do that with a object that extends the Thread class? Seems to me it is not because each time you instantiate the Thread class you are creating a new instance so each time you call start it would be on a unique copy of the method, not shared. Am I right? If you can synchronize a method on threads do you have an example?

Thanks!!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tim Holmes:
...you pass only one instance of a "runnable" object into the thread. Is it possible to do that with a object that extends the Thread class? ...

I'm not sure I understand what you're asking here. But note that Runnable is an interface. Thread is a class that implements Runnable, so any Thread is also an instance of Runnable.
Tim Holmes
Greenhorn

Joined: Aug 26, 2005
Posts: 25
Please see my code below. Here is my question...in Example A the code is sharing a copy of the MyRunnable class so it is sharing a variable "gear". I understand that. My question is on example B what is the point in synchronizing if the variable "gear" is not shared? Both threads are gauranteed to access the "gear" variable independently! In addition, can a run method on a class that extends thread ever be called by more than one thread? If so how?


Example A:


Example B:
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tim Holmes:
...My question is on example B what is the point in synchronizing if the variable "gear" is not shared? Both threads are gauranteed to access the "gear" variable independently! In addition, can a run method on a class that extends thread ever be called by more than one thread? ...

If a resource is not shared by different threads, then there is no need to synchronize access to it.

If you call run() directly, it just executes in the current thread. To execute as a separate thread, you need to call start(), which calls run() behind the scenes. Trying to call start() on the same thread more than once will throw an exception.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Threading
 
Similar Threads
Thread Question
question of threads from Dan's mock
Calling Thread By Start() and Run()
Threads and a local variable
Regarding Threads