Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

If the funId is the same,synchronized  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If three threads at the same time are trying to invoke this method.

public void test(String funId) {
// if the funId is the same,synchronized
// else not synchronized.
}

how should I do it?
 
author
Sheriff
Posts: 23565
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

eason liu wrote:If three threads at the same time are trying to invoke this method.

public void test(String funId) {
// if the funId is the same,synchronized
// else not synchronized.
}

how should I do it?



A quick and dirty way to accomplish this is to intern the string and synchronize on it.

But ... it is probably better to create a class just for this. This class will use synchronization to "lock", but it only hold the sync lock long enough to add the id into a collection (and the thread that owns it). If another thread owns it, then it will have to wait for the unlock (using the wait and notify mechanism, of course).

Henry
 
eason liu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I'm clear now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!