• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's the best way to synchronize?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a question about synchronization. I have a method that must be run synchronously, not just for the instance, but for all of my instances of this class that are in my pool. If I have a pool of java objects of class MyClass, and if I make tje non-static method on MyClass synchronized, calls to that method will only be synchronized on one instance from the pool, right? If it were a static method, I believe it would synchronize for all instances in the pool, but I don't want the method to be static. So to synchronize a method call when I have a pool of objects, is better to put a synchronize block in the method?

Thanks,
Ryan
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan P. Kane wrote:Hello,

I have a question about synchronization. I have a method that must be run synchronously, not just for the instance, but for all of my instances of this class that are in my pool. If I have a pool of java objects of class MyClass, and if I make tje non-static method on MyClass synchronized, calls to that method will only be synchronized on one instance from the pool, right? If it were a static method, I believe it would synchronize for all instances in the pool, but I don't want the method to be static. So to synchronize a method call when I have a pool of objects, is better to put a synchronize block in the method?

Thanks,
Ryan



You need to add the synchronized keyword to the code you want synchronized, yes. I'm not sure I understand beyond that what you are asking. Is you question about synchronizing a block of code vs the entire method?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan P. Kane wrote:So to synchronize a method call when I have a pool of objects, is better to put a synchronize block in the method?


Sounds good to me. How about
synchronized (myPool) { ...
?

[Edit] An alternative might be to add a synchronized method to the Pool class itself.

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic