Java Reference
In-Depth Information
Tip a semaphore is not limited to the number of permits it was created with. each release() method adds one
permit to it. therefore, if you call the release() method more than the times you call its acquire() method, you end
up having more permits than the one you started with. a permit is not acquired on a per thread basis. One thread
can acquire a permit from a semaphore and another can return it. this leaves the burden of the correct usage of
acquiring and releasing a permit on programmers. a semaphore has other methods to acquire a permit, which will let
you back off instead of forcing you to wait if a permit is not immediately available, such as tryAcquire() and
acquireUninterruptibly() methods.
Barriers
A barrier is used to make a group of threads meet at a barrier point. A thread from a group arriving at the barrier
waits until all threads in that group arrive. Once the last thread from the group arrives at the barrier, all threads in the
group are released. You can use a barrier when you have a task that can be divided into subtasks; each subtask can be
performed in a separate thread and each thread must meet at a common point to combine their results. Figure 6-8
through Figure 6-11 depict how a barrier synchronizer lets a group of three threads meet at the barrier point and lets
them proceed.
B
A
R
R
I
E
R
Figure 6-8. Three threads arrive at a barrier
B
A
R
R
I
E
R
Figure 6-9. One thread waits for two other threads to arrive at the barrier
 
 
Search WWH ::




Custom Search