I got into a situation where I have to synchronize a function based on input. eg. There is a function. func (int a) --- this function can be called by many threads. I want to block threads if they try to call this function with the same integer. Otherwise, let them continue without continuing. I wrote a function
HashTable<Integer> check=new Hashtable<Integer>(); func(int a){ Integer lock=check.get(a); if(lock==null){ check.put(a); }else{ check.put(a); } synchronized(lock){
}}
Please let me know if this is correct. Also, if there is another solution besides this. I try to understand. If I find an answer, I will send it.
source share