Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

syncronized methods  RSS feed

 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
if I use the keyword synchronized on a method of a class and 2 separate threads (in the same jvm) create an object from this class.
Which is correct:
1. Both threads cannot be inside the method in the same time (each thread has it's own instance of an object of course).
2. Because of the fact the objects are separate (although the class is the same) they method will not be synchronized and both threads can be inside of that method simulatiousely.

Thanks,
John.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to threads / synchronization.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on what object the method is synchronized on. If you use the synchronized keyword on the method declaration, then the object "this" is used. Therefore, if each thread has its own instance, then both threads can get into the method since there are two different objects -- two different "thisses". If your two threads are referencing the same instance, then only one can get in at a time.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Duh -- submitted extraneous response. Feel free to delete this...)
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do I make the method synchronized for all instances of this class?
I want no thread to go in in any instance of the class.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do I make the method synchronized for all instances of this class?

Simply you can make that method as static ... so that only one thread can execute the method .Even if there are two instance of the class ,even then only one thread can execute at a time.
OR
Class c = Class.forName("className");
synchronized (c) {
// implemtation
}
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never tried to make a static method synchronized -- I guess it uses the Class object to synchronize on. Note that if other static methods in that class are synchronized, then all methods become single threaded. I.e., if method A is being run, then method B is also locked, since both use the same object to synchronize on. (The same goes for any synchronized method, static or not -- all the methods block together since they all share the same locking object.) If you want to make a single method single threaded, then put a synchronized block within it, and use a singleton (such as a private static object) to synchronize on, and don't use that object for any other synchronize block.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!