What you're doing is not going to work. A call to an isLocked
method will only tell you that a lock was locked at some instant of time during the call. It does this by returning true. If it returns false, it has told you nothing at all. (As the ReentrantLock documentation hints, this can be useful in that if it returns true, you know some other thread somewhere does lock it; code to do so exists and sometimes executes. If it returns false, you know nothing. Call this method enough times, and you might get useful statistical information--which seems to be what this method is for.)
There's no limit to the possibilities. If isLocked
returns true, 500 threads could have locked it, done tons of work each, and released it before the statement after the call starts to execute. If it returns false, the same thing could have happened. And it either case you have no clue as to whether it's locked when your next statement starts to execute. (And if you knew it was locked at the start, you would know nothing about the situation 2 nanoseconds after the start.)
You need to rethink your synchronization a bit. Perhaps accept the need for multiple locking threads. Or, if you really want to pass locking responsibility from thread to thread, maybe some kind of relay-race-baton field, so each thread knows precisely what it's responsible for.