Is it better to sync with semaphores or monitors?

Is it better to sync with semaphores or with monitors?

+5
source share
4 answers

"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

  • It can be used anywhere in the program, but should not be used on the monitor.
  • Wait() Doesn't always block the caller (i.e. when the semaphore counter is greater than zero).
  • Signal() Either releases a blocked thread, if any, or increases the semaphore counter.
  • Signal() , .

  • Wait() .
  • Signal() , , , .
  • Signal() , ( Hoare) ( Mesa). , .

: http://www.cs.mtu.edu/~shene/NSF-3/e-Book/MONITOR/sema-vs-monitor.html

:

+14

, (synchronized blocks/methods) , , . . , , ?

, ( ), , → → . , , .

, - , , , . , , , 1, , , , . .

- . , reentrancy. Reentrancy , , , , . , , , , ; , .

, Java . , - - , .

, , . Java.

; , "", "" Java.

+7

, synchronized.

: , ?

  • . N , . .
  • , , (1) .

J2SE 5.0 concurrency . , :

  • , , .
  • , , , .
  • , . , .

, - - - - - . , .

+3

, , 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 .

+1
source

All Articles