gpt4 book ai didi

Java:生产者-消费者两个线程停止工作,卡住

转载 作者:行者123 更新时间:2023-12-01 16:25:53 27 4
gpt4 key购买 nike

我有这段代码,它从第一个数组中获取数据并将其放入第二个数组中,然后从第一个数组中将其删除。它工作正常一段时间,但随后停止,而第一个数组中仍然有要粘贴的值:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
private static String buffer;
private static String lock = "lock";

public static void main(String[] args) throws Exception {
Usb usb1 = new Usb(getListFromValues("1", "2", "3", "4", "5"));
Usb usb2 = new Usb(getListFromValues("10", "20", "30", "40", "50"));

Thread usbCutThread = new Thread(new UsbCutThread(usb1));
Thread usbPasteThread = new Thread(new UsbPasteThread(usb2));

usbCutThread.start();
usbPasteThread.start();

usbCutThread.join();
usbPasteThread.join();

}

static class UsbCutThread implements Runnable {
private Usb usb;
public UsbCutThread(Usb usb) {this.usb = usb;}

@Override
public void run() {
try {
System.out.println("inside cut");
for (int i = 0; i < usb.getData().size(); i++) {
buffer = usb.getValue();
System.out.println("cutting value " + buffer);

synchronized (lock) {
System.out.println("copied to buffer, waiting for paste");
lock.notify();
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("erasing");
usb.eraseValue();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}

static class UsbPasteThread implements Runnable {
private Usb usb;
public UsbPasteThread(Usb usb) {this.usb = usb;}

@Override
public void run() {
try {
while (true) {
System.out.println("inside paste");
//while it copies, cut thread can't erase - what if copy fails
synchronized (lock) {
usb.addValue("stuff");
System.out.println("pasted");
lock.notify();
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}

}
}
}
catch (Exception e) {
e.printStackTrace();
}
}

}

static class Usb {
List<String> data = new ArrayList<>();

public Usb(List<String> data) {this.data = data;}

public String getValue() {return data.get(data.size() - 1);}

public void addValue(String value) {
data.add(value);
}

public void eraseValue() {
data.remove(data.size() - 1);
}

public List<String> getData() {return data;}
}

public static ArrayList<String> getListFromValues(String... values) {
ArrayList<String> result = new ArrayList<>();
for (String v: values) {
result.add(v);
}
return result;
}
}

输出:

inside cut
inside paste
pasted
cutting value 5
copied to buffer, waiting for paste
inside paste
pasted
erasing
cutting value 4
copied to buffer, waiting for paste
inside paste
pasted
erasing
cutting value 3
copied to buffer, waiting for paste
inside paste
pasted
erasing

最佳答案

UsbCutThread0 迭代到 usb.getData().size():

              for (int i = 0; i < usb.getData().size(); i++) {

在循环中,您通过 usb.eraseValue()更改usb.getData()的大小。这显然会影响迭代量。在您的情况下,您将进行 3 次迭代,直到 i == usb.getData().size()true 并且退出循环。

当您将整个事情放入 while (true) 时,另一个线程进入 lock.wait() 。这是您的程序不会终止的地方。

要解决第一个问题,您只需迭代到固定限制:

              int n = usb.getData().size();
for (int i = 0; i < n; i++) {

我认为第二个问题实际上对你来说不是问题,而是你想要的。所以我就停在这里。

关于Java:生产者-消费者两个线程停止工作,卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62150112/

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