gpt4 book ai didi

java - 即使线程终止后锁也不会释放

转载 作者:行者123 更新时间:2023-12-01 19:50:23 25 4
gpt4 key购买 nike

在下面的程序中,不知何故程序没有结束。当我调试时,在检查 lock 变量期间显示该锁由生产者线程持有,如下所示

java.util.concurrent.locks.ReentrantLock@55584367[Locked by thread pool-1-thread-1] java.util.concurrent.locks.ReentrantLock$NonfairSync@5b9fb7bc[State = 8, empty queue]

public class LocksTest {

public static void main(String[] args) {
List<String> contents = new ArrayList<>();
Lock lock = new ReentrantLock();
String greenColor = "\u001B[32m";
String yellowColor = "\u001B[33m";
String redColor = "\u001B[31m";
MyProducer myProducer = new MyProducer(greenColor, contents, lock);
MyConsumer myConsumer1 = new MyConsumer(yellowColor, contents, lock);
MyConsumer myConsumer2 = new MyConsumer(redColor, contents, lock);

ExecutorService service = Executors.newFixedThreadPool(3);
service.execute(myProducer);
service.execute(myConsumer2);
service.execute(myConsumer1);
// new Thread(myProducer).start();
// new Thread(myConsumer1).start();
// new Thread(myConsumer2).start();
service.shutdown();
}
}

class MyProducer implements Runnable {

private String color;
private List<String> contents;
private Lock lock;

public MyProducer(String color,
List<String> contents,
Lock lock) {
this.color = color;
this.contents = contents;
this.lock = lock;
}

@Override
public void run() {

int count = 0;
while (true) {
if (lock.tryLock()) {
try {
contents.add("" + ++count);
System.out.println(color + "Adding " + count);
if (count == 5) {
System.out.println(color + "Adding " + "EOF");
contents.add("EOF");
break;
}

try {
Thread.sleep(1000l);
} catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();
}

} finally {
lock.unlock();
}
}
}
}
}

class MyConsumer implements Runnable {

private String color;
private List<String> contents;
private Lock lock;

public MyConsumer(String color,
List<String> contents,
Lock lock) {
this.color = color;
this.contents = contents;
this.lock = lock;
}

@Override
public void run() {
String value = null;
while (true) {
if (lock.tryLock()) {
try {
if (!contents.isEmpty()) {
Iterator<String> stringItr = contents.iterator();

while (stringItr.hasNext())
value = stringItr.next();
System.out.println(color + "Consuming " + value);
stringItr.remove();
}
if ("EOF".equals(value)) {
break;
}
} finally {
lock.unlock();
}
}
}
}
}

结果如下:

[32mAdding 1
[33mConsuming 1
[32mAdding 2
[33mConsuming 2
[32mAdding 3
[33mConsuming 3
[32mAdding 4
[33mConsuming 4
[32mAdding 5
[32mAdding EOF
[33mConsuming EOF
[31mConsuming 5

[32mAdding 1
[32mAdding 2
[32mAdding 3
[32mAdding 4
[32mAdding 5
[32mAdding EOF
[33mConsuming EOF
[31mConsuming 5
[31mConsuming 4
[31mConsuming 3
[31mConsuming 2
[31mConsuming 1

虽然结果符合预期,但我不确定为什么程序没有终止?其次,终止的线程可以持有(我通过使用注释的传统线程方式观察到这种奇怪的行为)?!

最佳答案

嗯,消费者和生产者共享一把锁对我来说有点奇怪,但你的问题是生产者放置了一个EOF,而你有2个消费者。所以消费者(不是你的生产者)无法打破 while 循环。

关于java - 即使线程终止后锁也不会释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51483264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com