Java Semaphore

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/24

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

25 Terms

1
New cards

What is a Semaphore in Java?

A concurrency utility that controls access to a resource by multiple threads using permits.

<p>A concurrency utility that controls access to a resource by multiple threads using permits.</p>
2
New cards

Which package includes Semaphore?

java.util.concurrent

3
New cards

What is the main purpose of Semaphore?

To limit the number of threads accessing a resource simultaneously.

4
New cards

How do you create a Semaphore with 3 permits?

new Semaphore(3)

5
New cards

What does acquire() do in Semaphore?

It acquires a permit, blocking if necessary until one becomes available.

6
New cards

What does release() do in Semaphore?

It releases a permit, increasing the number of available permits.

7
New cards

What happens if no permits are available in Semaphore?

Calling thread blocks until a permit is released.

8
New cards

What is a fair semaphore?

A semaphore that grants permits in the order of thread requests (FIFO).

9
New cards

How do you create a fair Semaphore?

new Semaphore(permits, true)

10
New cards

What is the difference between fair and non-fair Semaphore?

Fair ensures FIFO granting of permits; non-fair may grant out of order.

11
New cards

What method checks the number of available permits?

availablePermits()

12
New cards

What method attempts to acquire a permit without blocking?

tryAcquire()

13
New cards

How can you acquire multiple permits at once?

Using acquire(int permits) or tryAcquire(int permits)

14
New cards

Can acquire() be interrupted?

Yes, it throws InterruptedException if the thread is interrupted while waiting.

15
New cards

What happens if a thread calls release() without acquire()?

Permit count increases, possibly beyond initial permits (not recommended).

16
New cards

Is Semaphore reusable?

Yes, permits can be repeatedly acquired and released.

17
New cards

What is a real-world use case for Semaphore?

Controlling access to a limited number of connections in a connection pool.

18
New cards

What is the difference between Semaphore and Lock?

Semaphore controls number of threads accessing; Lock provides exclusive locking.

19
New cards

What is the difference between Semaphore and CountDownLatch?

Semaphore is reusable and dynamic; CountDownLatch is one-time and countdown only.

20
New cards

Can you use Semaphore to implement a mutex?

Yes, by creating a Semaphore with one permit.

21
New cards

Is release() method safe to call in finally block?

Yes, it's a best practice to release permits in a finally block.

22
New cards

What happens if a thread acquires more permits than available?

It blocks until enough permits are released by other threads.

23
New cards

Can a Semaphore be used for signaling between threads?

Not ideal; use CountDownLatch or Exchanger for better signaling mechanisms.

24
New cards

What are the key methods of Semaphore?

acquire(), release(), tryAcquire(), availablePermits(), drainPermits()

25
New cards

What does drainPermits() do?

Removes and returns all available permits immediately.