lock锁的几个特性
- lock是外部接口,synchronized是Java内部关键字。
- lock可以响应中断,如果当前持有锁则释放锁,sync不可
- lock可以尝试超时获取锁,sync不可
- lock可以获知是否有线程在等待当前的锁,sync不可
- lock需要手动释放锁,sync自动释放
lock锁等待过程响应中断。
package com.study.train;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
private Lock lock = new ReentrantLock();
public static void main(String[] args) {
Main test = new Main();
MyThread thread1 = new MyThread(test);
MyThread thread2 = new MyThread(test);
thread1.start();
thread2.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread2.interrupt();
}
public void insert(Thread thread) throws InterruptedException{
lock.lockInterruptibly();
try {
System.out.println(thread.getName()+"得到了锁");
long startTime = System.currentTimeMillis();
for( ; ;) {
if(System.currentTimeMillis() - startTime >= 64421)
break;
}
}
finally {
System.out.println(Thread.currentThread().getName()+"执行finally");
lock.unlock();
System.out.println(thread.getName()+"释放了锁");
}
}
}
class MyThread extends Thread {
private Main test = null;
public MyThread(Main test) {
this.test = test;
}
@Override
public void run() {
try {
test.insert(Thread.currentThread());
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"被中断");
}
}
}
lock持有锁时响应中断
package com.teriste.thread;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class ThreadInteruptDemo {
ReentrantLock lock1=new ReentrantLock();
ReentrantLock lock2=new ReentrantLock();
public void exeInterupt() throws Exception{
Thread t1=new Thread(new DemoThread(lock1,lock2));
Thread t2=new Thread(new DemoThread(lock2,lock1));
t1.start();
t2.start();
System.out.println(t1.getName()+"中断");
Thread.sleep(1000);
t1.interrupt();
Thread.sleep(10000);
}
Object syn1=new Object();
Object syn2=new Object();
public void exeInteruptSyn() throws Exception{
Thread t1=new Thread(new DemoThread1(syn1,syn2));
Thread t2=new Thread(new DemoThread1(syn2,syn1));
t1.start();
t2.start();
System.out.println(t1.getName()+"中断");
Thread.sleep(1000);
t1.interrupt();
Thread.sleep(100000);
}
static class DemoThread implements Runnable{
ReentrantLock lock1;
ReentrantLock lock2;
public DemoThread(ReentrantLock lock1,ReentrantLock lock2){
this.lock1=lock1;
this.lock2=lock2;
}
@Override
public void run() {
try {
lock1.lockInterruptibly();
TimeUnit.MILLISECONDS.sleep(200);
lock2.lockInterruptibly();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock1.unlock();
lock2.unlock();
System.out.println("线程"+Thread.currentThread().getName()+"正常结束");
}
}
}
static class DemoThread1 implements Runnable{
Object lock1;
Object lock2;
public DemoThread1(Object lock1,Object lock2){
this.lock1=lock1;
this.lock2=lock2;
}
@Override
public void run() {
try {
synchronized (lock1){
TimeUnit.MILLISECONDS.sleep(200);
synchronized (lock2){
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("线程"+Thread.currentThread().getName()+"正常结束");
}
}
}
}