aspose file tools
The moose likes Threads and Synchronization and the fly likes Any reason to synchronize a run method? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Any reason to synchronize a run method?" Watch "Any reason to synchronize a run method?" New topic
Author

Any reason to synchronize a run method?

James Davison
Greenhorn

Joined: Mar 28, 2004
Posts: 27
I ran across the following code which got me to thinking: Is there any good reason to synchronize a run method for a class that implements Runnable? Because this method implements a while(true) statement in the method body, a second invocation of the run method will never be executed. Perhaps that was the original developer's intention?

The same class contains the following method. Ordinarily, I would synchronize a method like this too, if I used an ArrayList. However, purgeQueue is Vector. For that reason, I would not think it necessary to synchronize the method itself (although I know it does not really hurt). Am I correct?

[ April 10, 2007: Message edited by: James Davison ]
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

They could have been trying to protect against running multiple threads on the same instance (like the following example). It might well be legitimate but it first impression is that of a shotgun approach to a threading bug.

Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2300
If you make run menthod syncronized then the lock of that class would be occupied before executing the run method.In case you start multiple threads using the same runnable implementation in the constructor of the Thread then it would work.But until the first thread finishes the second thread cannot start and until the second thread finishes the third cannot start as all the threads depends on lock of a single object.


Rahul Bhattacharjee
LinkedIn - Blog
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

As chris correctly said, its a shotgun approach of making a multi-threaded code a single threaded one
In effect, if you synchronize the run method, there are two outcomes:
1) Synchronize does not help, if the threads are created using different runnable instances.
2) There is no multi-threading, if all threads are created using the same runnable. Its as good as directly calling the run method from the same thread of execution
[ April 11, 2007: Message edited by: Nitesh Kant ]

apigee, a better way to API!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Nitesh Kant:

2) There is no multi-threading, if all threads are created using the same runnable. Its as good as directly calling the run method from the same thread of execution


Not exactly: the thread that started these threads will still be able to run in parallel. So it works like a working queue or something?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Rahul Bhattacharjee:
If you make run menthod syncronized then the lock of that class would be occupied before executing the run method.In case you start multiple threads using the same runnable implementation in the constructor of the Thread then it would work.But until the first thread finishes the second thread cannot start and until the second thread finishes the third cannot start as all the threads depends on lock of a single object.


Well, it could also happen that the third thread is executed before the second.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Ilja Preuss:


Not exactly: the thread that started these threads will still be able to run in parallel. So it works like a working queue or something?


Yeah, that is true, it behaves like a work queue processed by a single thread.
[ April 12, 2007: Message edited by: Nitesh Kant ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Any reason to synchronize a run method?
 
Similar Threads
Does a synchronized method lock on the whole object or just on his own method?
syncronizing run() method
Help: Basics of Thread
Question on synchronize block
synchronized( this ) vs synchronized(MyClass.class)