gpt4 book ai didi

java - java.util.concurrent.BrokenBarrierException 的原因

转载 作者:搜寻专家 更新时间:2023-10-31 20:27:07 37 4
gpt4 key购买 nike

我正在研究循环势垒。

我正在尝试编写代码示例:

public class Main {

public static final int PARSER_COUNT = 15;
public static final int PRODUCT_TRESHOLD = 5;

public static void main(String args[]) {
ProductImporter productImporter = new ProductImporter(PRODUCT_TRESHOLD);

for (int i = 0; i < PARSER_COUNT; i++) {
new Thread(new ProductParser(productImporter, "Parser" + (i + 1))).start();
}
}
}

class ProductImporter {

private CyclicBarrier barrier;
private List<String> parsedProducts;

public ProductImporter(int productTreshold) {
parsedProducts = new ArrayList<String>();
barrier = new CyclicBarrier(productTreshold, new Runnable() {
@Override
public void run() {
System.out.println("start import " + parsedProducts);
parsedProducts.clear();
}
});
}

public void recharge(String name) {
try {
parsedProducts.add(name);
/*System.out.println("Added product to importList#"+parsedProducts.size());*/
barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}

}

class ProductParser implements Runnable {

private String name;
private Random rand;
private ProductImporter productImporter;
private int counter = 0;

public ProductParser(ProductImporter productImporter, String name) {
this.name = name;
this.productImporter = productImporter;
this.rand = new Random();
}

public void run() {
try {
while (true) {
Thread.sleep(rand.nextInt(12));
System.out.println(name + " parsed product#" + counter);
productImporter.recharge(name + "#" + (counter++));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

但是我得到 java.util.concurrent.BrokenBarrierException

请帮助理解我做错了什么

附言

有时在输出中我会看到愚蠢的行:

start import [Parser7, Parser4, Parser9, Parser10, Parser2, Parser13, Parser6]

预期结果 - 包含 5 个元素的数组

附言

输出样本:

start import [Parser13#21, Parser15#28, Parser12#22, Parser6#19, Parser8#27]
Parser9 parsed product#23
Parser1 parsed product#19
Parser15 parsed product#29
Parser14 parsed product#23
Parser11 parsed product#22
start import [Parser9#23, Parser1#19, Parser15#29, Parser14#23, Parser11#22]
Parser12 parsed product#23
Parser8 parsed product#28
Parser4 parsed product#25
Parser13 parsed product#22
Parser5 parsed product#23
start import [Parser12#23, Parser8#28, Parser4#25, Parser13#22, Parser5#23]
Parser7 parsed product#23
Parser3 parsed product#26
Parser6 parsed product#20
Parser14 parsed product#24
Parser10 parsed product#25
start import [Parser7#23, Parser3#26, Parser6#20, Parser14#24, Parser10#25]
Parser2 parsed product#24
Parser4 parsed product#26
Parser8 parsed product#29
Parser1 parsed product#20
Parser5 parsed product#24
Parser13 parsed product#23
start import [Parser2#24, Parser4#26, Parser8#29, Parser1#20, Parser5#24]
Parser5 parsed product#25
Parser9 parsed product#24
Parser11 parsed product#23
Parser7 parsed product#24
start import [Parser5#25, Parser9#24, Parser11#23, Parser7#24]
Parser10 parsed product#26
Parser3 parsed product#27
Parser1 parsed product#21
Parser13 parsed product#24
Parser7 parsed product#25
start import [Parser10#26, Parser3#27, Parser1#21, Parser13#24, Parser7#25]
Parser2 parsed product#25
Parser10 parsed product#27
Parser7 parsed product#26
Parser15 parsed product#30
Parser12 parsed product#24
start import [Parser2#25, Parser10#27, Parser7#26, Parser15#30, Parser12#24]
Parser6 parsed product#21
Parser14 parsed product#25
Parser11 parsed product#24
Parser5 parsed product#26
Parser13 parsed product#25
Parser1 parsed product#22
Parser4 parsed product#27
Parser11 parsed product#25
Parser11 parsed product#26
Exception in thread "Thread-12" java.util.concurrent.BrokenBarrierException
at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207)
at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
at lection2.task3.ProductImporter.recharge(Main.java:46)
at lection2.task3.ProductParser.run(Main.java:74)
at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.BrokenBarrierException
at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250)
at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
at lection2.task3.ProductImporter.recharge(Main.java:46)
at lection2.task3.ProductParser.run(Main.java:74)
at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.BrokenBarrierException
at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250)
at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
at lection2.task3.ProductImporter.recharge(Main.java:46)
at lection2.task3.ProductParser.run(Main.java:74)
at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.BrokenBarrierException
at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207)
at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
at lection2.task3.ProductImporter.recharge(Main.java:46)
at lection2.task3.ProductParser.run(Main.java:74)
at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.BrokenBarrierException
at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207)
at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
at lection2.task3.ProductImporter.recharge(Main.java:46)
at lection2.task3.ProductParser.run(Main.java:74)
at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.BrokenBarrierException
at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250)
at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
at lection2.task3.ProductImporter.recharge(Main.java:46)
at lection2.task3.ProductParser.run(Main.java:74)
at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.BrokenBarrierException
at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:250)
at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
at lection2.task3.ProductImporter.recharge(Main.java:46)
at lection2.task3.ProductParser.run(Main.java:74)
at java.lang.Thread.run(Thread.java:745)
java.util.concurrent.BrokenBarrierException
at java.util.concurrent.CyclicBarrier.dowait(CyclicBarrier.java:207)
at java.util.concurrent.CyclicBarrier.await(CyclicBarrier.java:362)
at lection2.task3.ProductImporter.recharge(Main.java:46)
at lection2.task3.ProductParser.run(Main.java:74)
at java.lang.Thread.run(Thread.java:745)

P.P.P.S

如果使 sleep 间隔更好 - 效果很好。

最佳答案

你运行 action throws ConcurrentModificationException

因为 ArrayList 不是合适的容器。使用同步块(synchronized block)保护对其的访问或使用线程安全容器,如 Vector

PS:您的 ArrayList 中有超过 5 个元素,因为没有任何东西可以保护它不被更改。

关于java - java.util.concurrent.BrokenBarrierException 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31882118/

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