0

I have the following code

public CampaignProcessorEntity processCampaign(CampaignProcessorEntity campaignEnity) {
 synchronized(campaignEnity.getHash()) {
        ....
        ....
 }
}

My code inside synchronised block run for minutes ,my requirement is ,if first thread starts executing the block ,the second one should wait ,only if both hash are same or else continue without synchronisation.I have different instance for each thread.The getHash() method returns new String object everytime.So i had a static map ,which stores the hash as key and value,and second thread on arrival checks the hash map and retrieve the monitor used by first thread ,still it does't wait.

Don Jose
  • 1,448
  • 2
  • 13
  • 27

1 Answers1

0

You can check first if hash values are same or not according the result u can goes for sync or not.

 synchronized(Object lock) {
        // check for hash values.
 }

if(hashValuesAreEqual) {

    synchronized(campaignEnity.getHash()) {
        // do your sync thing.
    }

}
else {
       // do your async thing.
}
  • doesn't synchronized(campaignEnity.getHash()) implicitly means the same? – Don Jose May 14 '18 at 09:56
  • This is piece of pseudo code. You have a static map so you don't have to compute hash value in sync block. –  May 14 '18 at 10:03
  • I'm saying about if(hashValuesAreEqual) condition ,both hash will be same value ,but stored in different string objects – Don Jose May 14 '18 at 10:06