简体   繁体   中英

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. 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

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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