How to use locks/synchronization here
- by MasterGberry
I have this code block here and i need to make sure the rankedPlayersWaitingForMatch is synchronized between threads properly. I was going to use synchronize but that i don't think will work here because of the variable being used in the if statement. I read online about final Lock lock = new ReentrantLock(); but I am a bit confused on how to use it in this case properly with the try/finally block. Can I get a quick example? Thanks
// start synchronization
if (rankedPlayersWaitingForMatch.get(rankedType).size() >= 2) {
Player player1 = rankedPlayersWaitingForMatch.get(rankedType).remove();
Player player2 = rankedPlayersWaitingForMatch.get(rankedType).remove();
// end synchronization
// ... I don't want this all to be synchronized, just after the first 2 remove()
} else {
// end synchronization
// ...
}