Forums Register Login

Threading

+Pie Number of slices to send: Send
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?


+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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??
+Pie Number of slices to send: Send
 

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 ]
+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
 

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.
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 871 times.
Similar Threads
Thread Question
Threads and a local variable
Calling Thread By Start() and Run()
question of threads from Dan's mock
Regarding Threads
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:04:01.