Java线程在相同数据上执行

时间:2012-10-06 15:22:18

标签: java multithreading

首先这里是代码,你可以只复制一个粘贴

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,或者发生错误计数,奇怪的是如果我得到错误,并修改代码,之后它将再次工作一次。 你能解释一下为什么会这样吗?

2 个答案:

答案 0 :(得分:2)

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

BTW查看java.util.concurrent包中的Executors,你不必为这些东西使用原始线程。同样使用Executors可能会让您更好地了解将Task中的内容与其他内容分开。

答案 1 :(得分: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)));
}