只有在锁没有被其他线程持有的时候才获取锁
在锁没有被其他线程持有的时候获取锁并返回true值, 将锁计数器置为1. 即使已将此锁设置为使用公平排序策略,如果该锁可用,则调用tryLock()将立即获取该锁,而不管其他线程当前是否正在等待该锁. 这种”恶霸式(barging)”的行为在某些情况下会很有用,即使它打破了公平. 如果你想保持这个锁的公正性, 则使用tryLock(0, TimeUnit.SECONDS)(这个方法同事也侦测中断).
如果当前线程已经持有该锁,那么锁计数器自增1并且该方法返回true
如果该锁被其他线程持有,那么直接返回false
Specified by: tryLock() in Lock
Returns:
当该锁未被持有或已被当前线程持有时返回true,否则返回fasle
Acquires the lock only if it is not held by another thread at the time of invocation.
Acquires the lock if it is not held by another thread and returns immediately with the value true, setting the lock hold count to one. Even when this lock has been set to use a fair ordering policy, a call to tryLock() will immediately acquire the lock if it is available, whether or not other threads are currently waiting for the lock. This “barging” behavior can be useful in certain circumstances, even though it breaks fairness. If you want to honor the fairness setting for this lock, then use tryLock(0, TimeUnit.SECONDS) which is almost equivalent (it also detects interruption).
If the current thread already holds this lock then the hold count is incremented by one and the method returns true.
If the lock is held by another thread then this method will return immediately with the value false.
Specified by: tryLock() in Lock
Returns:
true if the lock was free and was acquired by the current thread, or the lock was already held by the current thread; and false otherwise