java - Java:生产者-消费者两个线程停止工作,冻结
问题描述
我有这段代码,它从第一个数组中获取数据并将其放入第二个数组中,然后将其从第一个数组中删除。它可以正常工作一段时间,但随后停止,而第一个数组中仍有要粘贴的值:
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
解决方案
UsbCutThread
0
从到迭代usb.getData().size()
:
for (int i = 0; i < usb.getData().size(); i++) {
在循环中,您正在更改usb.getData()
by的大小usb.eraseValue()
。这显然会影响迭代的数量。在您的情况下,您将获得 3 次迭代,直到i == usb.getData().size()
istrue
并且您退出循环。
lock.wait()
当您将整个内容放入while (true)
. 这是您的程序不会终止的地方。
要解决第一个问题,您只需迭代到固定限制:
int n = usb.getData().size();
for (int i = 0; i < n; i++) {
我认为第二个问题实际上对您来说不是问题,并且是您想要的。所以我就停在这里。
推荐阅读
- javascript - 静态使用博客图片
- python - Python中的列表变量
- python - 为什么无法识别 Python-Bottle Restful 中的完整 URL
- python - 在积极的后视中使用积极的前瞻?
- c# - 我不知道写什么来防止数据库 C# 中的重复/相同记录
- django - 保存另一个表单时如何更新另一个模型字段
- android - 即使数据库设置为 fallbackToDestructiveMigration,RoomDatabase 也会在迁移期间崩溃
- java - 使用 math.random 和 while 循环的简单 Java 彩票模拟器?
- sql - 如何使用 Merge 同步目标表
- python - 如何将我的方法子调用到我的函数中?