尝试获取锁,如果获取到了执行同步的地方,如果获取不到那么返回的是false将做其他的事情。
public class TestTryLcok implements Runnable{
private static Lock locks = new ReentrantLock();
@Override
public void run() {
try {
if(locks.tryLock(4, TimeUnit.SECONDS)){
System.out.println(Thread.currentThread().getName()+"-->");
Thread.sleep(6000);
}else{
System.out.println(Thread.currentThread().getName()+" time out ");
}
} catch (InterruptedException e) {
// e.printStackTrace();
}finally {
locks.unlock();//会抛出锁对象的异常,因为没有获取锁在unlock的时候出异常,可以先判断一下是否存在在执行。
}
}
public static void main(String[] args) throws InterruptedException {
TestTryLcok test =new TestTryLcok();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.start();
t2.start();
t1.join();
System.out.println("over");
}
}