1

首先是代码,你可以复制粘贴

import java.util.ArrayList;

public class RepetionCounter implements Runnable{
private int x;
private int y;
private int[][] matrix;
private int xCounter;
private int yCounter;
private ArrayList<Thread> threadArray;
private int rowIndex;
private boolean[] countCompleted; 

public RepetionCounter(int x, int y, int [][]matrix)
{
    this.x = x;
    this.y = y;
    this.matrix = matrix;
    this.threadArray = new ArrayList<Thread>(matrix.length);
    this.rowIndex = 0;
    for(int i = 0; i < matrix.length; i++){
        threadArray.add(new Thread(this));
    }
    countCompleted = new boolean[matrix.length];
}

public void start(){
    for (int i = 0; i < threadArray.size(); i++){
        threadArray.get(i).start();
        this.rowIndex++;
    }
}

public void count(int rowIndex)
{
    for(int i = 0; i < matrix[rowIndex].length; i++){
        if (matrix[rowIndex][i] == x){
            this.xCounter++;
        } else if (matrix[rowIndex][i] == y){
            this.yCounter++;
        }
    }
}

@Override
public void run() {
    count(this.rowIndex);
    countCompleted[this.rowIndex] = true;
}

public int getxCounter() {
    return xCounter;
}

public void setxCounter(int xCounter) {
    this.xCounter = xCounter;
}

public int getyCounter() {
    return yCounter;
}

public void setyCounter(int yCounter) {
    this.yCounter = yCounter;
}

public boolean[] getCountCompleted() {
    return countCompleted;
}

public void setCountCompleted(boolean[] countCompleted) {
    this.countCompleted = countCompleted;
}

public static void main(String args[]){
    int[][] matrix = {{0,2,1}, {2,3,4}, {3,2,0}};
    RepetionCounter rc = new RepetionCounter(0, 2, matrix);
    rc.start();
    boolean ready = false;
    while(!ready){
        for(int i = 0; i < matrix.length; i++){
            if (rc.getCountCompleted()[i]){
                ready = true;
            } else {
                ready = false;
            }
        }
    }
    if (rc.getxCounter() > rc.getyCounter()){
        System.out.println("Thre are more x than y");
    } else {System.out.println("There are:"+rc.getxCounter()+" x and:"+rc.getyCounter()+" y");

    }
}

}

我想要这段代码做什么:我给对象一个矩阵和两个数字,我想知道这两个数字在矩阵中出现了多少次。我创建了与矩阵的行数一样多的线程(这就是为什么会有那个 ArrayList),所以在这个对象中我有 k 个线程(假设 k 是行数),每个线程都计算两者的出现次数数字。问题是:如果我第一次运行它,一切正常,但是如果我再次尝试执行它,我得到了 IndexOutOfBoundException,或者出现次数错误,奇怪的是,如果我得到错误,并修改代码,之后它将再次工作一次。你能向我解释为什么会这样吗?

4

2 回答 2

2

您需要为每个线程提供自己的 Runnable。让它们都共享同一个 Runnable 将导致灾难性的竞争条件。将每个线程需要执行的逻辑分离到一个 Runnable 中。然后将启动线程的部分代码移动到 Runnable 之外的位置。

顺便说一句,查看 java.util.concurrent 包中的 Executors,你不必为这些东西使用原始线程。此外,使用 Executors 可以让您更好地了解将进入任务的内容与其他内容分开。

于 2012-10-06T15:29:58.680 回答
2

RepetitionCounter为每个线程使用相同的实例:

for(int i = 0; i < matrix.length; i++){
    threadArray.add(new Thread(this));
}

所以他们都将共享相同的rowIndex。代码本身非常混乱,所以我建议您将线程的逻辑封装在一个单独的Runnable类中,并使用单独的行 ID:

class ThreadTask implements Runnable {
    private int rowId;
    private int[][] matrix;

    public ThreadTask(int[][] matrix, int rowId) {
        this.matrix = matrix; // only a reference is passed here so no worries
        this.rowId = rowId;
    }

    public void run() {
       // search on my row
    }
}

然后:

for(int i = 0; i < matrix.length; i++) {
     threadArray.add(new Thread(new ThreadTask(matrix, i)));
}
于 2012-10-06T15:30:18.210 回答