• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized keyword on a run() method  RSS feed

 
Jules Bach
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I understood this - turns out perhaps i don't!

if I use the synchronized keyword on a thread's run method..i.e.



And then create multiple instances of the above thread and start them from another class (person is my thread class - it implements runnable)

eg..



The use of the synchronized keyword in the run() has no effect..I thought that whichever thread acquired the lock first would need to complete {some work} before the next thread could begin executing {some work}.

this doesn't seem to be the case..both jeremy and james start up - and both are able to exist in the {some work} critical section at the same time.

i saw another post on this topic in these forum, but I couldn't follow the answer..

Help appreciated,

Jules
[ June 11, 2008: Message edited by: Jules Bach ]
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jules,

in your example the two runnables (james + jeremy) are given as an argument to the thread constructors. These create two independent threads which are "initialized" with the run() methods of the runnable objects and after this you start the threads. So a new thread of execution is created by the JVM with a starting point in the run() method of james and jeremy.

This is somewhat different to the situation where you have two already active threads which want to access the same synchronized method! Of course you could manually call the run() methods of the two persons after all this code. Then the synchronized keyword would have the desired effect. But calling the run() method manually from some other code is totally different than using it for the initialization of a thread object. If you'd call it from your code the JVM wouldn't create any new thread of execution.

Marco
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A synchronized method locks on the current instance of the class. So when the jeremy.run() method is called it locks on the jeremy object and any other thread that tries to call the run() method on the jeremy object will be blocked. However, your second thread is calling the run method on the james object. No other thread is currently calling the run method on the james object, so there is no blocking.
To do what you want to do, you would need something likeHere, the syncObject is shared between all instances of the class, so when jeremy.run() is called, it will obtain the lock on syncObject and start doing it's stuff. james.run() is then called, will try to get the lock on syncObject, find it's already locked and will block until jeremy.run() has finished.
[ June 11, 2008: Message edited by: Joanne Neal ]
 
Jules Bach
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Marco and Joanne - that makes sense now

cheers,

Jules
 
Nomaan Butt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great explanation, thanks...
 
Sid Shankar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can anyone shed some light on this? What if I wanted to use "syncObject" in a non-static context? How can I remove the "static" modifier and still be able to use syncObject? Or is it not possible.

Thanks in advance.

Sid.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sid Shankar wrote:Hi, can anyone shed some light on this? What if I wanted to use "syncObject" in a non-static context? How can I remove the "static" modifier and still be able to use syncObject? Or is it not possible.


Well, first of all, it is perfectly fine to use a static variable in a non-static context -- why do you think that you need to have instance variables to be used by instance methods?

Second, this is all moot. You need to get the synchronization correct. If two threads need to synchronize with each other, then they need to be the same instance. And using static variables is one way to insure it -- but it does *not* have to be. If you want to use instance variable, instead of static variables, for whatever reason, then fine -- just make sure that the code is thread safe.

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