Is it better to sync with semaphores or with monitors?
"Better" depends on the context. According to James MacParnen, they are "equally strong." I highly recommend looking at his blog to discuss the differences .
Here is a quick guide that I found:
semaphores
Wait()
Signal()
: http://www.cs.mtu.edu/~shene/NSF-3/e-Book/MONITOR/sema-vs-monitor.html
:
, (synchronized blocks/methods) , , . . , , ?
synchronized
, ( ), , → → . , , .
, - , , , . , , , 1, , , , . .
- . , reentrancy. Reentrancy , , , , . , , , , ; , .
, Java . , - - , .
, , . Java.
; , "", "" Java.
, synchronized.
: , ?
J2SE 5.0 concurrency . , :
, - - - - - . , .
, , JDK . Java, . Java Tiger (5.0), concurrency. , , java.util.concurrent.
, , , . , . , Lock: , JDK, ReentrantLock, , ReentrantReadWriteLock, / .
, / (, ).
, ( , JDK, Semaphore, CyclicBarrier, CountdownLatch Exchanger). , , , , . , .
More information can be found in this book: “ Java Concurrency in Practice ” and in this IBM tutorial: “ Concurrency in JDK 5.0 ”. Some more nice examples can be found here .