Java线程在相同数据上的执行

Java线程在相同数据上的执行,java,multithreading,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 in

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

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");

    }
}

}
import java.util.ArrayList;
公共类RepetionCounter实现可运行{
私人INTX;
私营企业;
私有int[][]矩阵;
专用int xCounter;
私人内部计数器;
私有数组列表线程数组;
私有索引;
私有布尔[]计数已完成;
公共重复计数器(整数x,整数y,整数[][]矩阵)
{
这个.x=x;
这个。y=y;
这个矩阵=矩阵;
this.threadArray=新的ArrayList(matrix.length);
this.rowIndex=0;
对于(int i=0;irc.getyCounter()){
System.out.println(“Thre大于y”);
}else{System.out.println(“有:“+rc.getxCounter()+”x和:“+rc.getyCounter()+”y”);
}
}
}
我想让这段代码做的是:我给对象一个矩阵和两个数字,我想知道这两个数字在矩阵中出现了多少次。我创建的线程数等于矩阵的行数(这就是为什么会有ArrayList),因此在这个对象中我有k个线程(假设k是行数),每个线程都计算这两个数字的出现次数。 问题是:如果我第一次运行它,一切正常,但如果我在另一次尝试执行它,我会得到和IndexOutOfBoundException,或者出现错误计数,奇怪的是,如果我得到错误,并修改代码,之后它会再次正常工作一次。
您能解释一下为什么会发生这种情况吗?

您需要为每个线程指定自己的可运行线程。让他们共享相同的Runnable将导致灾难性的比赛条件。将每个线程需要执行的逻辑分离为可运行的。然后将启动线程的代码部分移动到Runnable之外的位置


顺便说一句,查看java.util.concurrent包中的执行器,您不必为这些内容使用原始线程。此外,使用执行器可能会让您更好地了解如何将任务中的内容与其他内容区分开来。

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


顺便说一句,查看java.util.concurrent包中的执行器,您不必为这些内容使用原始线程。另外,使用执行器可以更好地区分任务中的内容和其他内容。

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

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

for(int i=0;i
您正在为每个线程使用相同的
重复计数器实例:

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

for(int i=0;i
谢谢你,我按照你说的做了尝试,效果很好,我想知道是否有办法让线程安全地共享数据(这正是我想要做的)@AR89:这是一个复杂的话题。您可以拥有数据的单个共享副本,在这种情况下,如果多个线程想要修改数据,您将需要锁,或者您可以拥有数据分区,就像在这种情况下,您不需要特殊的同步。谢谢,我按照您所说的做了尝试,并且成功了,我想知道是否有办法让线程安全地共享数据(这就是我想做的)@AR89:这是一个复杂的话题。你可以拥有数据的一个共享副本,在这种情况下,如果多个线程想要修改数据,你将需要锁,或者你可以拥有数据分区,就像在这种情况下,你不需要特殊的同步。
for(int i = 0; i < matrix.length; i++) {
     threadArray.add(new Thread(new ThreadTask(matrix, i)));
}