reentrantlock用于替代synchronized
* 使用reentrantlock可以完成同样的功能
* reentrantlock必须要手动释放锁
* 使用syn锁定的话如果遇到异常,jvm会自动释放锁,但是lock必须手动释放锁,
* 因此经常在finally中进行锁的释放
public class RLDemo1 {
Lock lock = new ReentrantLock();
public void test1(){
try {
lock.lock();//this
for (int i = 0; i < 3; i++) {
System.out.println(i);
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void test2(){
lock.lock();
System.out.println("test 2...");
lock.unlock();
}
public static void main(String[] args) {
RLDemo1 rlDemo1 = new RLDemo1();
new Thread(rlDemo1::test1).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(rlDemo1::test2).start();
}
}
public class RLDemo2 {
Lock lock = new ReentrantLock();
public void test1(){
try {
lock.lock();
for (int i = 0; i < 2; i++) {
System.out.println(i);
TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
/**
* 使用reentrantlock可以进行“尝试锁定”tryLock,这样无法锁定,或者在指定时间内无法锁定,
* 线程可以决定是否继续等待
* 可以使用tryLock进行尝试锁定,不管锁定与否,方法都将继续执行
* 可以根据tryLock的返回值来判定是否锁定
* 也可以指定tryLock的时间,由于tryLock(time)抛出异常,所以要注意unclock的处理,必须放到finally中
*/
public void test2(){
boolean locked = false;
try {
locked = lock.tryLock(3, TimeUnit.SECONDS);
System.out.println("test2...." + locked);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (locked) {
System.out.println("test2 end");
lock.unlock();
}
}
}
public static void main(String[] args) {
RLDemo2 rlDemo2 = new RLDemo2();
new Thread(rlDemo2::test1).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(rlDemo2::test2).start();
}
}
public class RLDemo3 {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
Thread t1 = new Thread(()->{
try {
lock.lock();
System.out.println("t1 start");
TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
System.out.println("t1 end");
} catch (InterruptedException e) {
System.out.println("interrupted!");
} finally {
lock.unlock();
}
});
t1.start();
Thread t2 = new Thread(()->{
boolean locked = false;
try {
// lock.lock();
//使用lockInterruptibly来锁定可以对Interrupt方法作出响应
lock.lockInterruptibly();
System.out.println("t2 start");
TimeUnit.SECONDS.sleep(5);
System.out.println("t2 end");
locked = true;
} catch (InterruptedException e) {
System.out.println("interrupted!");
} finally {
if (locked){
lock.unlock();
}
}
});
t2.start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.interrupt();
}
}
public class RLDemo4 extends Thread{
//ReentrantLock可以指定是否为公平锁,true为公平,默认为false
private static ReentrantLock lock = new ReentrantLock(true);
@Override
public void run() {
for (int i = 0; i < 100 ; i++) {
lock.lock();
try {
System.out.println(Thread.currentThread().getName() + "获得锁");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
RLDemo4 rlDemo4 = new RLDemo4();
Thread t1 = new Thread(rlDemo4);
Thread t2 = new Thread(rlDemo4);
t1.start();
t2.start();
}
}