重入锁,在需要同步的代码部分进行锁定,使用完毕之后一定要释放锁。
实例方式:
ReentrantLock lock = new ReentrantLock(true) ;
后面有个 Boolean 类型的 fair 参数,是标识该锁是否公平的,默认 不公平,公平的意思就是 锁的给与基于调用顺序,先调用的,先获取,不公平则是遵循cpu调用规则,默认不公平效率更高。
举个栗子:
public class Main3 implements Callable {
private ReentrantLock lock = new ReentrantLock(true) ;
private int i =10;
@Override
public String call() throws Exception {
lock.lock();
System.out.println( Thread.currentThread().getName() + "==>" + --i );
lock.unlock();
return "" ;
}
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(10);
Main3 task = new Main3();
for (int i = 0; i < 10; i++) {
pool.submit( task );
}
pool.shutdown();
}
}
实例方式:
private ReentrantLock lock = new ReentrantLock(true) ;
private Condition condition = lock.newCondition();
举个栗子:
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Main4 {
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void method1(){
try {
lock.lock();
System.out.println("当前线程:" + Thread.currentThread().getName() + "进入等待状态..");
Thread.sleep(3000);
System.out.println("当前线程:" + Thread.currentThread().getName() + "释放锁..");
condition.await(); // Object wait
System.out.println("当前线程:" + Thread.currentThread().getName() +"继续执行...");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void method2(){
try {
lock.lock();
System.out.println("当前线程:" + Thread.currentThread().getName() + "进入..");
Thread.sleep(3000);
System.out.println("当前线程:" + Thread.currentThread().getName() + "发出唤醒..");
condition.signal(); //Object notify
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("当前线程:" + Thread.currentThread().getName() + "执行完毕");
lock.unlock();
}
}
public static void main(String[] args) {
final Main4 main4 = new Main4();
new Thread(new Runnable() {
@Override
public void run() {
main4.method1();
}
} , "t1").start();
new Thread(new Runnable() {
@Override
public void run() {
main4.method2();
}
} , "t2").start();
}
}
执行结果:
当前线程:t1进入等待状态..
当前线程:t1释放锁..
当前线程:t2进入..
当前线程:t2发出唤醒..
当前线程:t2执行完毕
当前线程:t1继续执行...
分析:
t1线程先开始执行,获得重入锁,执行method1,输出第一句,然后休眠之后输出第二句,紧接着释放锁,然后 t2获得锁,执行method2,输出第三句,然后第二唤醒 其他的线程,此操作和 notify 一样不释放锁,所以 t2执行完毕之后,t1 才继续执行
例如:
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class UseManyCondition {
private ReentrantLock lock = new ReentrantLock();
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
public void m1(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m1等待..");
c1.await();
System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m1继续..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m2(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m2等待..");
c1.await();
System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m2继续..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m3(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m3等待..");
c2.await();
System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m3继续..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m4(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");
c1.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m5(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");
c2.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
final UseManyCondition umc = new UseManyCondition();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
umc.m1();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
umc.m2();
}
},"t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
umc.m3();
}
},"t3");
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
umc.m4();
}
},"t4");
Thread t5 = new Thread(new Runnable() {
@Override
public void run() {
umc.m5();
}
},"t5");
t1.start(); // c1
t2.start(); // c1
t3.start(); // c2
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t4.start(); // c1
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t5.start(); // c2
}
}
读写锁ReentrantReadWriteLock,其核心就是实现读写分离的锁。在高并发访问下,尤其是读多写少的情况下,性能要远高于重入锁。
之前学synchronized、ReentrantLock时,我们知道,同一时间内,只能有-个线程进行访问被锁定的代码,那么读写锁则不同,其本质是分成两个锁,即读锁、写锁。在读锁下,多个线程可以并发的进行访问,但是在写锁的时候,只能一个一个的顺序访问。
口诀:读读共享,写写互际,读写互斥。
实例方式:
private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
private ReadLock readLock = rwLock.readLock();
private WriteLock writeLock = rwLock.writeLock();
public class Main4 {
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
public void read(){
readLock.lock();
try{
System.out.println("当前线程:" + Thread.currentThread().getName() + "进入...");
Thread.sleep(3000);
System.out.println("当前线程:" + Thread.currentThread().getName() + "退出...");
}catch (Exception e){
}finally {
readLock.unlock();
}
}
public static void main(String[] args) {
final Main4 main4 = new Main4();
new Thread(new Runnable() {
@Override
public void run() {
main4.read();
}
} , "t1").start();
new Thread(new Runnable() {
@Override
public void run() {
main4.read();
}
} , "t2").start();
new Thread(new Runnable() {
@Override
public void run() {
main4.read();
}
} , "t3").start();
}
}
执行结果:
当前线程:t1进入...
当前线程:t2进入...
当前线程:t3进入...
当前线程:t1退出...
当前线程:t2退出...
当前线程:t3退出...
可以看到,读锁,其实是并发执行的,同时进入同时退出,若果换成 重入锁则就是 进入出来,进入出来这样了
代码跟上面一个,只是把 read 里面的 readLock 改为 writeLock,效果和 重入锁一样,所以读锁适用于 写多读少操作