简体   繁体   English

AtomicInteger行为异常

[英]AtomicInteger misbehavior

I have an exercise: given a matrix of AtomicIntegers (initialized to 0), I have to run a thread for each rows, and a thread for each column, on the rows I subtract 1, on the columns I add 1, so at the end the matrix should remain as it was. 我有一个练习:给定AtomicIntegers矩阵(初始化为0),我必须为每行运行一个线程,为每列运行一个线程,在我减去1的行上,在我加1的列上运行,所以在最后,矩阵应保持原样。 The problem is that the matrix change! 问题在于矩阵发生了变化! Here is the code: The object takes the matrix, a boolean which will tell him to operate on columns or rows, and the index of the column/row where it must operate. 这是代码:对象采用矩阵,一个布尔值(将告诉他对列或行进行操作)以及必须对其进行操作的列/行的索引。

import java.util.concurrent.atomic.AtomicInteger;

public class FifthExerciseSafe implements Runnable{
    private Thread thread;
    private boolean onRows;//tells if the object operates on the rows or on the columns
    private AtomicInteger[][] matrix;
    private int index;

public FifthExerciseSafe(AtomicInteger[][] matrix, boolean onRows, int index){
    this.matrix = matrix;
    this.onRows = onRows;
    this.index = index;
}

public void start(){
    thread = new Thread(this);
    thread.start();
}

public boolean isOnRows() {
    return onRows;
}

public void setOnRows(boolean onRows) {
    this.onRows = onRows;
}

public AtomicInteger[][] getMatrix() {
    return matrix;
}

public void setMatrix(AtomicInteger[][] matrix) {
    this.matrix = matrix;
}

@Override
public void run() {
    if (this.onRows){
        for(int i = 0; i < matrix[index].length; i++){
            matrix[index][i].decrementAndGet();
        }
    } else {            
        for(int i = 0; i < matrix.length; i++){
            matrix[i][index].incrementAndGet();
        }
    }
}//run

public static void main(String args[]){
    AtomicInteger[][] m = new AtomicInteger[3][3];
    for (int i = 0; i < m.length; i++){
        for(int j = 0; j < m.length; j++){
            m[i][j]= new AtomicInteger(0);
        }
    }

    for(int i = 0; i < 3; i++){//I create 6 objects, 3 for columns and 3 for rows
        FifthExerciseSafe fes1 = new FifthExerciseSafe(m, true, i);
        FifthExerciseSafe fes2 = new FifthExerciseSafe(m, false, i);
        fes1.start();
        fes2.start();
    }
    for(int i = 0; i < m.length; i++){
        for(int j = 0; j < m.length; j++){
            System.out.print(m[i][j]+" ");
        }
    }
}//main
}

The output should be: 000000 but sometimes it is: -100-100-1-10 输出应为:000000,但有时为:-100-100-1-10

It only happens on a netbook with an Intel Atom, on a desktop with a Core Duo I haven't seen it yet, but I'm wondering if it could happen there too. 它仅在带有Intel Atom的上网本上,在具有Core Duo的台式机上发生,但我还没有看到,但是我想知道它是否也可能在那里发生。

You have started the threads, but you never wait for them to finish before going on to print out the matrix. 您已经启动了线程,但是在继续打印矩阵之前,您无需等待线程完成。 You need to use Thread.join for each started thread, which will block until the thread is done. 您需要为每个启动的线程使用Thread.join ,它将阻塞直到线程完成。 Only then will it be safe to print out the results. 只有这样,才能安全地打印出结果。 For example, introduce an ArrayList into which you add all the instances of Thread you start, and then have a separate for loop that invokes join on each instance. 例如,引入一个ArrayList在其中add您启动的Thread所有实例,然后有一个单独的for循环,该循环在每个实例上调用join

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM