java lock的tryLock为什么总返回true?

Lock lock = new ReentrantLock();  
lock.lock();

// 此处为何返回true,不应该被锁了吗?
System.out.println(lock.tryLock()); // =>true
 
lock.unlock();
 
System.out.println(lock.tryLock());   // => true
阅读 3.6k
1 个回答

ReentrantLock.tryLock()

Returns:
true if the lock was free and was acquired by the current thread, or the lock was already held by the current thread; andfalse otherwise

ReentrantLock.tryLock() 在当前线程已经持有锁的时候,返回 true

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题