gpt4 book ai didi

java - 根据邻居的值更新多线程单元。如何继续使用 CyclicBarrier?

转载 作者:行者123 更新时间:2023-12-01 19:39:13 26 4
gpt4 key购买 nike

我正在努力实现以下目标:

  1. 获取用户的两个输入( lengthamountOfCycles )
  2. 创建一个包含 length 的数组线程数量。每个包含一个整数 value[1, 100] 范围内.
  3. 循环amountOfCycles + 1次数并在每次迭代中执行以下操作:
    1. 打印数组的值。
    2. 根据其(循环)邻居更新数组中的每个值:
      • 如果该值小于两个邻居的值:将值增加 1
      • 如果该值大于两个邻居的值:将值减少 1
      • 如果当前值小于或等于一个邻居,并且大于或等于另一个邻居:保持该值不变

根据邻居更新这些值是它是多线程的原因。 请注意,这只是练习多线程的东西。通过简单地删除所有线程并创建数组的副本 ( which I already did ),我可以轻松完成上述操作。

这是迄今为止我的代码:

import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Main{
Cell[] cells;
CyclicBarrier barrier;
int length, amountOfCycles;

Main(){
Scanner stdin = new Scanner(System.in);
length = stdin.nextInt();
amountOfCycles = stdin.nextInt();
barrier = new CyclicBarrier(length);
cells = new Cell[length];
for(int i=0; i<length; i++)
cells[i] = new Cell(i);
}

public static void main(String[] args){
Main program = new Main();
program.start();
}

void start(){
for(int i=0; i<length; i++)
cells[i].run();

for(int cycle = amountOfCycles; cycle >= 0; cycle--)
System.out.println(Arrays.toString(cells));
}

class Cell implements Runnable{
int value,
index;

Cell(int i){
index = i;
value = (int)(Math.random() * 100) + 1; // Random integer within the range [1, 100]
}

@Override
public void run(){
try{
// Wait for the start of the cycle:
barrier.wait();

// Determine the increment for the value of this cell:
// Get the values of the neighbors:
int valueLeftNeighbor = cells[(length - index - 1) % length].value,
valueRightNeighbor = cells[(index + 1) % length].value,
// And create an increment-integer with default value 0:
increment = 0;
// If the current value is smaller than that of both neighbors:
if(value < valueLeftNeighbor && value < valueRightNeighbor){
// Increase the current value by 1
increment = 1;
}
// If the current value is larger than that of both neighbors:
if(value > valueLeftNeighbor && value > valueRightNeighbor){
// Decrease the current value by 1
increment = -1;
}
// If the current value is smaller than or equal to one neighbor,
// and larger than or equal to the other neighbor:
// Leave the value the same (so increment stays 0)

// Wait until every cell is done calculating its new value:
barrier.await();

// And then actually update the values of the cells
value += increment;
}catch(Exception ex){
System.err.println("Exception occurred! " + ex);
ex.printStackTrace();
}
}

@Override
public String toString(){
return Integer.toString(value);
}
}
}

基于this SO question and answerits accepted answer .

我上面的代码目前的作用:

它打印具有随机值的数组 amountOfCycles + 1次,但不会改变周期之间的任何值。这是由于IllegalMonitorStateExceptions我明白了。可能是因为我需要一个 synchronized(barrier){ ... }某处,因为 barrier在类(class)Main而不是Cell ?将其添加到 run -Cell的方法然而,类会导致程序不再打印任何内容,也不会终止..

Here in my code above in an online compilers to see the current (incorrect) result.

我期望它做什么:

每次循环后修改数组中的值。

最佳答案

让我们回顾一下你的推理:

问题 1

为了在任何对象上调用 wait(),当前线程必须拥有其监视器。您正在调用barrier.wait(),而没有任何synchronized(barrier)。

这就是为什么你会得到IllegalMonitorStateException

问题 2

添加同步部分会导致您的程序挂起,因为您没有创建任何线程。对 Runnable 调用 run 会在同一线程中同步执行它。没有其他线程可以调用 notify

问题 3

您可能不想调用Object.wait,而是调用CyclicBarrier.await()。因此,对 Object.wait() 所需的同步的讨论不是所需解决方案的一部分,我添加它只是为了澄清。

关于java - 根据邻居的值更新多线程单元。如何继续使用 CyclicBarrier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55956526/

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