Semaphore Class Flashcards

1
Q

Constructor to create Semaphore objects with a given number of permits (the number of threads that can access the resource at a time). If the permit’s value is negative, the given number of release() calls must happen before acquire() calls can succeed.

A

Semaphore(int permits)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Same as the Semaphore(int permits) constructor, but this extra fair option indicates that the permits should be allotted on a first come first served basis.

A

Semaphore(int permits, boolean fair)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Acquires a permit if available; blocks until a permit becomes available. Can throw an InterruptedException if some other thread interrupts it while waiting to acquire a permit. Overloaded version takes a number of permits to acquire as an argument.

A

void acquire()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Same as the acquire() method but the thread cannot be interrupted while waiting to acquire a permit.

A

void acquireUninterruptibly()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Acquires a permit from the semaphore if it is available at the time of the call and returns true; if unavailable, it returns false immediatly (without blocking). Overloaded version takes a timeout period. It will block trying to acquire the permit until the timeout period.

A

boolean tryAcquire()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Releases a permit from the semaphore. The overloaded version specifies the number of permits to release.

A

void release()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly