Skip to main content
Spelling and grammar; block-quote quoted code comment
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

YouYour threadLocal prevents a thread from unlocking a lock taken by a different thread. That's not good, asthereas there are legitimate uses for this scenario (some producer-consumer).

What I also dislike is silently ignoring the unlock request. If it's forbidden, then it should probably throw.

Anyway, you can what you did without any thread local just by using AtomicReference<Thread> (with the content being the locking thread, or null, if it's free).

//To change body of catch statement use File | Settings | File Templates.
//To change body of catch statement use File | Settings | File Templates.

Should I?

You threadLocal prevents a thread from unlocking a lock taken by a different thread. That's not good, asthere are legitimate uses for this scenario (some producer-consumer).

What I also dislike is silently ignoring the unlock request. If it's forbidden, then it should probably throw.

Anyway, you can what you did without any thread local just by using AtomicReference<Thread> (with the content being the locking thread, or null, if it's free).

//To change body of catch statement use File | Settings | File Templates.

Should I?

Your threadLocal prevents a thread from unlocking a lock taken by a different thread. That's not good, as there are legitimate uses for this scenario (some producer-consumer).

What I also dislike is silently ignoring the unlock request. If it's forbidden, then it should probably throw.

Anyway, you can what you did without any thread local just by using AtomicReference<Thread> (with the content being the locking thread, or null, if it's free).

//To change body of catch statement use File | Settings | File Templates.

Should I?

Source Link
maaartinus
  • 13.7k
  • 1
  • 36
  • 75

You threadLocal prevents a thread from unlocking a lock taken by a different thread. That's not good, asthere are legitimate uses for this scenario (some producer-consumer).

What I also dislike is silently ignoring the unlock request. If it's forbidden, then it should probably throw.

Anyway, you can what you did without any thread local just by using AtomicReference<Thread> (with the content being the locking thread, or null, if it's free).

//To change body of catch statement use File | Settings | File Templates.

Should I?