This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If the funId is the same,synchronized

 
eason liu
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?
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic