This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

What's the best way to synchronize?  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • 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: 10748
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!