Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8862701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:47:29+00:00 2026-06-14T15:47:29+00:00

I have an exercise: given a matrix of AtomicIntegers (initialized to 0), I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:47:30+00:00Added an answer on June 14, 2026 at 3:47 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple exercise where I call a Thread and subsequently the run()
I have been given an exercise to solve the zebra puzzle using a constraint
I have been given an exercise about anagrams and it looked so really easy
I'm just doing an exercise where I have a list of files (given as
I have the exercise with following code int FindFirstSet(unsigned BitMap, unsigned start) { unsigned
During my university exercise I have come across strange behaviour of a variable. /*
I have been busy with a exercise where I need to compare a winning
i have to crawl last.fm for users (university exercise). I'm new to python and
Let's say I have a large query (for the purposes of this exercise say
I have a code like this, which is solved by me following a exercise

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.