Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threading

 
Tim Holmes
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holmes
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic