Park & Unpark
// 暂停当前线程
LockSupport.park();
// 恢复某个线程的运行
LockSupport.unpark(暂停线程对象)
park & unpark 是以线程为单位来【阻塞】和【唤醒】线程,如LockSupport.unpark(t1),而 notify 只能随机唤醒一个等待线程,notifyAll是唤醒所有等待线程,就不那么【精确】
park & unpark 可以先 unpark,而 wait & notify 不能先 notify
状态切换:
多把锁
好处,是可以增强并发度
坏处,如果一个线程需要同时获得多把锁,就容易发生死锁
class BigRoom {
private final Object studyRoom = new Object();
private final Object bedRoom = new Object();
public void sleep() {
synchronized (bedRoom) {
log.debug("sleeping 2 小时");
Sleeper.sleep(2);
}
}
public void study() {
synchronized (studyRoom) {
log.debug("study 1 小时");
Sleeper.sleep(1);
}
}
}
死锁
有这样的情况:一个线程需要同时获取多把锁,这时就容易发生死锁
import static java.lang.Thread.sleep;
public class DeadLockTest {
public static void main(String[] args) {
Object A = new Object();
Object B = new Object();
Thread t1 = new Thread(() -> {
synchronized (A) {
System.out.println("lock A");
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (B) {
System.out.println("lock B");
}
}
}, "t1");
Thread t2 = new Thread(() -> {
synchronized (B) {
System.out.println("lock B");
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (A) {
System.out.println("lock A");
}
}
},"t2");
t1.start();
t2.start();
}
}
避免死锁要注意加锁顺序
定位死锁
terminal中输入jps和jstack + 进程
活锁
活锁出现在两个线程互相改变对方的结束条件,最后谁也无法结束
import static java.lang.Thread.sleep;
public class AliveLockTest {
private static int count = 10;
public static void main(String[] args) {
new Thread(() -> {
// 期望减到 0 退出循环
while (count > 0) {
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
count--;
System.out.println(count);
}
}, "t1").start();
new Thread(() -> {
// 期望超过 20 退出循环
while (count < 20) {
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
System.out.println(count);
}
}, "t2").start();
}
}
饥饿
很多教程中把饥饿定义为,一个线程由于优先级太低,始终得不到 CPU 调度执行,也不能够结束
ReentrantLock
// 获取锁
reentrantLock.lock();
try {
// 临界区
} finally {
// 释放锁
reentrantLock.unlock();
}
可重入
可重入是指同一个线程如果首次获得了这把锁,那么因为它是这把锁的拥有者,因此有权利再次获取这把锁
如果是不可重入锁,那么第二次获得锁时,自己也会被锁挡住
public class ReentrantLock extends java.util.concurrent.locks.ReentrantLock {
static ReentrantLock lock = new ReentrantLock();
static ReentrantLock lock2 = new ReentrantLock();
public static void main(String[] args) {
method1();
}
public static void method1() {
lock.lock();
try {
System.out.println("execute method1");
method2();
} finally {
lock.unlock();
}
}
public static void method2() {
lock2.lock();
try {
System.out.println("execute method2");
method3();
} finally {
lock2.unlock();
}
}
public static void method3() {
lock.lock();
try {
System.out.println("execute method3");
} finally {
lock.unlock();
}
}
}
import static java.lang.Thread.sleep;
public class ReentrantLock extends java.util.concurrent.locks.ReentrantLock {
static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
System.out.println("t1启动...");
try {
lock.lockInterruptibly();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("等锁的过程中被打断");
return;
}
try {
System.out.println("t1获得了锁");
} finally {
lock.unlock();
}
}, "t1");
lock.lock();
System.out.println("main获得了锁");
t1.start();
try {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("执行打断");
t1.interrupt();
} finally {
lock.unlock();
System.out.println("main释放锁");
}
}
}
注意如果是不可中断模式,那么即使使用了 interrupt 也不会让等待中断
18:06:56.261 [main] c.TestInterrupt - 获得了锁
18:06:56.265 [t1] c.TestInterrupt - 启动...
18:06:57.266 [main] c.TestInterrupt - 执行打断 // 这时 t1 并没有被真正打断, 而是仍继续等待锁
18:06:58.267 [main] c.TestInterrupt - 释放了锁
18:06:58.267 [t1] c.TestInterrupt - 获得了锁
锁超时
import java.util.concurrent.locks.ReentrantLock;
import static java.lang.Thread.sleep;
public class TimeoutLock extends ReentrantLock {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
Thread t1 = new Thread(() -> {
System.out.println("启动...");
if (!lock.tryLock()) {
System.out.println("获取立刻失败,返回");
return;
}
try {
System.out.println("获得了锁");
} finally {
lock.unlock();
}
}, "t1");
lock.lock();
System.out.println("获得了锁");
t1.start();
try{
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}