我正在尝试完成以下任务:
length
和amountOfCycles
)length
个线程。每个都包含value
范围内的整数[1, 100]
。amountOfCycles + 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 answer和its accepted answer。
我上面的代码当前正在做什么:
它以amountOfCycles + 1
次的随机值打印数组,但在两次循环之间不更改任何值。这是由于我得到的IllegalMonitorStateExceptions
。可能是因为我在某个地方需要synchronized(barrier){ ... }
,是因为barrier
在类Main
中而不是Cell
中吗?但是,将其添加到run
类的Cell
方法中会使该程序不再打印任何内容,也不会终止。
Here in my code above in an online compilers to see the current (incorrect) result.
我希望它做什么:
每个循环后修改数组中的值。
答案 0 :(得分:2)
让我们回顾一下您的推理:
问题1
为了在任何对象上调用wait(),当前线程必须拥有其监视器。您正在调用barrier.wait(),而没有任何已同步(barrier)。
这就是为什么您获得IllegalMonitorStateException
问题2
添加同步部分会导致程序挂起,因为您没有创建任何线程。在Runnable上调用run在同一线程中同步执行它。没有其他线程可以调用notify
问题3
您可能不想呼叫Object.wait
,而要呼叫CyclicBarrier.await()
。因此,Object.wait()
所需的同步讨论不是所需解决方案的一部分,我仅出于澄清目的添加了它。
答案 1 :(得分:1)
有一些问题。
1)您没有创建线程。您可以像这样从Runnable创建线程:
Thread t = new Thread(runnable); //create thread
t.start(); //start the thread
更改代码:
for(int i=0; i<length; i++)
cells[i].run();
对于这样的事情:
for (int i = 0; i < length; i++)
new Thread(cells[i]).start();
2)在您实际上没有实现任何循环来获得一个循环的每个循环之后,您都不会打印该数组。要在每个循环之后打印数组,请创建新的Runnable,当所有线程到达循环屏障时都会调用此方法,您可以直接将此Runnable设置为循环屏障
请更改您的代码:
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);
对于这样的事情:
Scanner stdin = new Scanner(System.in);
length = stdin.nextInt();
amountOfCycles = stdin.nextInt();
cells = new Cell[length];
for (int i = 0; i < length; i++)
cells[i] = new Cell(i);
barrier = new CyclicBarrier(length, () -> {
System.out.println(Arrays.toString(cells)); //code that will run every time when all thread reach the cyclic barrier
});
3)在线程中创建循环:
更改代码:
try{
// Wait for the start of the cycle:
barrier.wait(); //remove this, you never called notify so its useless
//business logic omitted
// 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();
}
对于这样的事情:
int cycleCounter = 0;
while (cycleCounter < amountOfCycles) {
cycleCounter++;
try {
//business logic omitted
barrier.await();
// And then actually update the values of the cells
value += increment;
} catch (Exception ex) {
System.err.println("Exception occurred! " + ex);
ex.printStackTrace();
}
}