Martijn
Martijn

Reputation: 2326

ReentrantReadWriteLock limit

Is there a limit to the ReentrantReadWriteLock? I'm now testing my application and it seems my write lock isn't giving out locks anymore (returning true: tryLock()) when I'm at 20 threads.

Upvotes: 1

Views: 1063

Answers (2)

Affe
Affe

Reputation: 47994

The limit is 65535 locks, so it seems unlikely you have hit it with 20 threads. (Although possible, the limit does count re-entrant acquisitions. However, violating it results in an error being thrown, not a false return value from tryLock().)

What is more likely is you're at a point where the system is busy enough that it's unlikely for a lock to be available at that exact instant when you attempt tryLock(). You should probably switch to a call that blocks with a reasonable timeout so that the thread waits until others are done and the lock becomes available.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533880

(returning true: tryLock()) when I'm at 20 threads.

Unless you have 20 CPUs, these threads are not running all at the same time, and the threads which are running are likely to be staving out the threads which are not. If you need to avoid this, you have to use a fair lock.

A better solution is to not write code where it is significant contention on any lock.

Upvotes: 0

Related Questions