How to use locks/synchronization here
Posted
by
MasterGberry
on Stack Overflow
See other posts from Stack Overflow
or by MasterGberry
Published on 2013-07-01T16:10:21Z
Indexed on
2013/07/01
16:21 UTC
Read the original article
Hit count: 220
java
|multithreading
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
// ...
}
© Stack Overflow or respective owner