I'm trying to prevent race condition and also want to don't affect system performance as could as possible. If two signal(data) arrive to server at the same time, handler starting to process these simultaneously(multi-thread), ıf signals have differents ID's there is no problem, but if they have different datas with same ID, for first signal need to be call "create_map_and_insert" method and second one need to be call "insert_to_exist_map" method. But in that condition both signals enters the if block at the same time, and cause exception (map already exist for this ID).
Is there a way to block other threads to reach here before main thread finish it's job for that specific situation? I don't want to use "synchronized" for every condition, just want to use for if signals have same ID's.
Thanks
Example code (JAVA):
SOTable testtable= SOTable.getInstance();
if (testtable.findvalue(ID) == null){
// Both signals enters here, but actually after the processing of first signal
// second one need to be enter to else if block.
testtable.create_map_and_insert.(ID,data); // testtable.findvalue(ID not null anymore
// testtable.findvalue(ID) not equal NULL any more
}
else if (testtable.findvalue(ID) != null){
//...
//
testtable.insert_to_exist_map.(ID,data);
}