活锁
与死锁一样,活锁同样会发生在多个相互协作的线程间,当他们为了彼此间的响应而相互礼让,使得没有一个线程能够继续前进,那么就发生了活锁。同死锁一样,发生活锁的线程无法继续执行。
相当于两个在半路相遇的人:出于礼貌他们相互礼让,避开对方的路,但是在另一条路上又相遇了。就这样,不停地一直避让下去。。。。
活锁代码示例
package com.derucci.admin;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.IntBinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* @author: zzq
* @Date: 2022/2/22
* @Time: 10:39
* @Version:
* @Description:
*/
public class JavaTest {
public static void main(String[] args) throws Exception {
ReentrantLock breadLock = new ReentrantLock();
ReentrantLock waterLock = new ReentrantLock();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(
() -> {
try {
// 尝试获得bread锁
while (breadLock.tryLock(10, TimeUnit.SECONDS)) {
System.out.println("拿到面包了,等着拿水。。。");
TimeUnit.SECONDS.sleep(1);
// 判断water是否被锁住,如果锁住,释放bread锁,再次尝试
if (waterLock.isLocked()) {
breadLock.unlock();
continue;
}
// 尝试获得water锁
if (!waterLock.tryLock(10, TimeUnit.SECONDS)) {
// 如果没有获得到,释放bread锁,再次尝试
breadLock.unlock();
continue;
}
System.out.println("拿到水了");
break;
}
System.out.println("吃面包,喝水");
} catch (InterruptedException e) {
System.out.println("线程中断");
} finally {
breadLock.unlock();
waterLock.unlock();
}
}
);
executorService.submit(
() -> {
try {
// 尝试获得water锁
while (waterLock.tryLock(10, TimeUnit.SECONDS)) {
System.out.println("拿到水了,等着拿面包");
TimeUnit.SECONDS.sleep(1);
// 判断bread是否被锁住,如果锁住,释放water锁,再次尝试
if (breadLock.isLocked()) {
waterLock.unlock();
continue;
}
// 尝试获得bread锁
if (!breadLock.tryLock(10, TimeUnit.SECONDS)) {
// 如果没有获得到,释放water锁,再次尝试
waterLock.unlock();
continue;
}
System.out.println("拿到面包了");
break;
}
System.out.println("喝水,吃面包");
} catch (InterruptedException e) {
System.out.println("线程中断");
} finally {
waterLock.unlock();
breadLock.unlock();
}
}
);
}
}
程序运行结果:
拿到面包了,等着拿水。。。
拿到水了,等着拿面包
拿到面包了,等着拿水。。。
拿到水了,等着拿面包
拿到水了,等着拿面包
拿到面包了,等着拿水。。。
拿到水了,等着拿面包
拿到面包了,等着拿水。。。
拿到水了,等着拿面包
解决活锁
锁让出的时候添加随机睡眠时间
package com.derucci.admin;
import org.junit.Test;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.IntBinaryOperator;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* @author: zzq
* @Date: 2022/2/22
* @Time: 10:39
* @Version:
* @Description:
*/
public class JavaTest {
public static void main(String[] args) throws Exception {
ReentrantLock breadLock = new ReentrantLock();
ReentrantLock waterLock = new ReentrantLock();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.submit(
() -> {
try {
// 尝试获得bread锁
while (breadLock.tryLock(10, TimeUnit.SECONDS)) {
System.out.println("拿到面包了,等着拿水。。。");
TimeUnit.SECONDS.sleep(1);
// 判断water是否被锁住,如果锁住,释放bread锁,再次尝试
if (waterLock.isLocked()) {
breadLock.unlock();
// 避免活锁
TimeUnit.MILLISECONDS.sleep(1000);
continue;
}
// 尝试获得water锁
if (!waterLock.tryLock(10, TimeUnit.SECONDS)) {
// 如果没有获得到,释放bread锁,再次尝试
breadLock.unlock();
continue;
}
System.out.println("拿到水了");
break;
}
System.out.println("吃面包,喝水");
} catch (InterruptedException e) {
System.out.println("线程中断");
} finally {
breadLock.unlock();
waterLock.unlock();
}
}
);
executorService.submit(
() -> {
try {
// 尝试获得water锁
while (waterLock.tryLock(10, TimeUnit.SECONDS)) {
System.out.println("拿到水了,等着拿面包");
TimeUnit.SECONDS.sleep(1);
// 判断bread是否被锁住,如果锁住,释放water锁,再次尝试
if (breadLock.isLocked()) {
waterLock.unlock();
// 避免活锁
TimeUnit.MILLISECONDS.sleep(1500);
continue;
}
// 尝试获得bread锁
if (!breadLock.tryLock(10, TimeUnit.SECONDS)) {
// 如果没有获得到,释放water锁,再次尝试
waterLock.unlock();
continue;
}
System.out.println("拿到面包了");
break;
}
System.out.println("喝水,吃面包");
} catch (InterruptedException e) {
System.out.println("线程中断");
} finally {
waterLock.unlock();
breadLock.unlock();
}
}
);
}
}
程序运行结果:
拿到面包了,等着拿水。。。
拿到水了,等着拿面包
拿到面包了
喝水,吃面包
拿到面包了,等着拿水。。。
拿到水了
吃面包,喝水