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

  • SEARCH
  • Home
  • 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 8866371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:46:05+00:00 2026-06-14T16:46:05+00:00

I’ve been researching this all night and haven’t found a solution so if anyone

  • 0

I’ve been researching this all night and haven’t found a solution so if anyone can help me I’d really appreciate it! I’m probably missing something super obvious. This is an assignment to understand synchronization where we’re taking a previous assignment where we used threads to multiply 2 matrices. In the previous assignment each thread multiplied a row so there were as many threads as there were rows.

In this assignment we’re only supposed to use 5 threads-all threads are supposed to start with one row/column and once the thread is complete it should choose the next available row/column using synchronization so now two threads will end up doing the same column.

This question helped get me in the right direction but I must be doing something wrong with the implementation because so far I have only gotten the program to either:

  1. only do the first 5 rows-the 5 threads perform once, each of them calculating a row or
  2. I added a loop (which is now commented out in my code) so the thread would keep performing but when I do that only the first thread does any work.

This is my class with my main and a couple of helper methods:

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;

public class MatrixMult {

public static void main(String[] args){
    int[][] matrixA;
    int[][] matrixB;
    int colA = 0;
    int rowA = 0;
    int colB = 0;
    int rowB = 0;
    Scanner userInput = new Scanner( System.in );
    System.out.println("Please enter the dimensions of matrix A");

    do{
        System.out.print("column for matrix A: ");
        colA = userInput.nextInt();
        System.out.println();
    } while(!validDimension(colA));

    rowB = colA;

    do{
        System.out.print("row for matrix A: ");
        rowA = userInput.nextInt();
        System.out.println();
    } while(!validDimension(rowA));

    matrixA = new int[rowA][colA];

    System.out.println("Please enter the dimensions of matrix B:");
    do{
        System.out.print("column for matrix B: ");
        colB = userInput.nextInt();
        System.out.println();
    } while(!validDimension(colB));

    matrixB = new int[rowB][colB];


    fillMatrix(matrixA);
    fillMatrix(matrixB);

    System.out.println("Would you like to print out matrix A and B? (y/n)");
    String userResponse = userInput.next();
    if(userResponse.equalsIgnoreCase("y")){
        System.out.println("Matrix A:");
        printBackMatrix(matrixA);
        System.out.println();
        System.out.println("Matrix B:");
        printBackMatrix(matrixB);
        System.out.println();
    }


    int[][] matrixProduct3 = multMatrixWithThreadsSync(matrixA, matrixB);

    String fileName = "C:/matrix.txt";
    System.out.println("Matrix product is being written to "+fileName);
    try {
        printMatrixToFile(matrixProduct3, fileName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static int[][] multMatrixWithThreadsSync(int[][] matrixA, int[][] matrixB) {

    int[][] matrixProduct = new int[matrixA.length][matrixB[0].length];
    int[] matrixProductColumn = new int[matrixA.length];

    Runnable task = new MultMatrixByRow(matrixA, matrixB, matrixProduct);

    for(int i=0; i<5; i++){

        Thread worker = new Thread(task);
        worker.start();
//          System.out.println(worker.getName());
        try {
            worker.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return matrixProduct;
}

private static void printMatrixToFile(int[][] matrix, String fileName) throws IOException{
    PrintWriter userOutput = new PrintWriter(new FileWriter(fileName));
    for(int i=0; i<matrix.length; i++){
        for(int j=0; j<matrix[0].length; j++){
            userOutput.print(matrix[i][j]+" ");
        }
        userOutput.println();
    }
    userOutput.close();

}

private static void printBackMatrix(int[][] matrix) {
    for(int i=0; i<matrix.length; i++){
        for(int j=0; j<matrix[0].length; j++){
            System.out.print(matrix[i][j]+" ");
        }
        System.out.println();
    }
}

private static void fillMatrix(int[][] matrix) {
    Random rand = new Random();

    for(int i=0; i<matrix.length; i++){
        for(int j=0; j<matrix[0].length; j++){
            matrix[i][j] = rand.nextInt(100) + 1;
        }
    }

}

public static boolean validDimension(int dim){
    if (dim <= 0 || dim >1000){
        System.err.println("Dimension value entered is not valid");
        return false;
    }
    return true;

}
}

and this is my class with runnable:

public class MultMatrixByRow implements Runnable {
private int i;
private int[][] matrixA;
private int[][] matrixB;
private int[][] matrixProduct;

public MultMatrixByRow(int[][] A, int[][] B, int[][] C) {
    this.matrixA = A;
    this.matrixB = B;
    this.matrixProduct = C;
}

@Override   
public void run(){
//      while(i < matrixProduct.length){
        int rowToWork = 0;
        synchronized (this){
 //             System.out.println("i is "+i);
            if ( i < matrixProduct.length){
                rowToWork = i;
                i++;
            }
            else{
                return;
            }
        }
        for(int j = 0; j < matrixB[0].length; j++){
            for(int k=0; k < matrixA[0].length; k++){
                matrixProduct[rowToWork][j] += matrixA[rowToWork][k]*matrixB[k][j];
            }
        }
//      }
        }
    }

Again-any help would really be appreciated! Thanks so much.

  • 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-14T16:46:06+00:00Added an answer on June 14, 2026 at 4:46 pm
    1. You are not synchronizing on a ressource, you need to share a lock object (in a static context or by constructor)
    2. I am not really able to figure out what in your programm should be synchronized when you don’t even let them work synchronously…. You start a thread and directly wait for him to stop.
      I think you have to start them all firstly and then in another loop call the join on every thread.

    Also, I am not quite sure what your Threads should work out seperately, I think they all work out the whole product matrix. You need to share a variable used to identify the already processed rows, which you access synchronized.

    I could fix your code but I would appreciate that you do that work yourself, since it’s a task to understand thread concurrency.

    EDIT: Explanation of synchronized:
    Synchronized takes a object as a lock, which only one thread can hold the monitor for it. When having the monitor for the lock, the thread can process the block, if not, he has to wait to get the monitor.
    In your case, you could use private static final Object lock = new Object(); as lock, you will synchronize on.

    EDIT 2: I completely constructed your code
    I’m not that proud to get all of your work done, but doesn’t matter, here it is.

    package anything.synchronize_stackoverflow_post;
    
    /**
     * @date 21.11.2012
     * @author Thomas Jahoda
     */
    public class ConcurrentMatrixMultiplyingTask implements Runnable {
    
        private int[][] matrixA;
        private int[][] matrixB;
        private int[][] matrixProduct;
        //
        private final ConcurrencyContext context;
    
        public ConcurrentMatrixMultiplyingTask(ConcurrencyContext context, int[][] A, int[][] B, int[][] C) {
            if (context == null) {
                throw new IllegalArgumentException("context can not be null");
            }
            this.context = context;
            this.matrixA = A;
            this.matrixB = B;
            this.matrixProduct = C;
        }
    
        @Override
        public void run() {
            while (true) {
                int row;
                synchronized (context) {
                    if (context.isFullyProcessed()) {
                        break;
                    }
                    row = context.nextRowNum();
                }
                System.out.println(Thread.currentThread().getName() + " is going to process row " + row);
                // i'm not really sure if this matrix algorithm here is right, idk..
                for (int j = 0; j < matrixB[0].length; j++) {
                    for (int k = 0; k < matrixA[0].length; k++) {
                        matrixProduct[row][j] += matrixA[row][k] * matrixB[k][j];
                    }
                }
            }
        }
    
        public static class ConcurrencyContext {
    
            private final int rowCount;
            private int nextRow = 0;
    
            public ConcurrencyContext(int rowCount) {
                this.rowCount = rowCount;
            }
    
            public synchronized int nextRowNum() {
                if (isFullyProcessed()) {
                    throw new IllegalStateException("Already fully processed");
                }
                return nextRow++;
            }
    
            public synchronized boolean isFullyProcessed() {
                return nextRow == rowCount;
            }
        }
    }
    

    And the ProcessingTask

    package anything.synchronize_stackoverflow_post;
    
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     * @date 21.11.2012
     * @author Thomas Jahoda
     */
    public class MatrixMulti {
    
        public static void main(String[] args) {
            int[][] matrixA;
            int[][] matrixB;
            int colA = 0;
            int rowA = 0;
            int colB = 0;
            int rowB = 0;
            Scanner userInput = new Scanner(System.in);
            System.out.println("Please enter the dimensions of matrix A");
    
            do {
                System.out.print("column for matrix A: ");
                colA = userInput.nextInt();
                System.out.println();
            } while (!validDimension(colA));
    
            rowB = colA;
    
            do {
                System.out.print("row for matrix A: ");
                rowA = userInput.nextInt();
                System.out.println();
            } while (!validDimension(rowA));
    
            matrixA = new int[rowA][colA];
    
            System.out.println("Please enter the dimensions of matrix B:");
            do {
                System.out.print("column for matrix B: ");
                colB = userInput.nextInt();
                System.out.println();
            } while (!validDimension(colB));
    
            matrixB = new int[rowB][colB];
    
    
            fillMatrix(matrixA);
            fillMatrix(matrixB);
    
            System.out.println("Would you like to print out matrix A and B? (y/n)");
            String userResponse = userInput.next();
            if (userResponse.equalsIgnoreCase("y")) {
                System.out.println("Matrix A:");
                printBackMatrix(matrixA);
                System.out.println();
                System.out.println("Matrix B:");
                printBackMatrix(matrixB);
                System.out.println();
            }
    
    
            int[][] matrixProduct3 = multMatrixWithThreadsSync(matrixA, matrixB);
    
            String fileName = "test.txt";
            System.out.println("Matrix product is being written to " + fileName);
            try {
                printMatrixToFile(matrixProduct3, fileName);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        private static int[][] multMatrixWithThreadsSync(int[][] matrixA, int[][] matrixB) {
    
            int[][] matrixProduct = new int[matrixA.length][matrixB[0].length];
            int[] matrixProductColumn = new int[matrixA.length];
            //
            ConcurrentMatrixMultiplyingTask.ConcurrencyContext context = new ConcurrentMatrixMultiplyingTask.ConcurrencyContext(matrixProduct.length);
            //
            Runnable task = new ConcurrentMatrixMultiplyingTask(context, matrixA, matrixB, matrixProduct);
            Thread[] workers = new Thread[5];
            for (int i = 0; i < workers.length; i++) {
                workers[i] = new Thread(task, "Worker-"+i);
            }
            for (int i = 0; i < workers.length; i++) {
                Thread worker = workers[i];
                worker.start();
            }
            for (int i = 0; i < workers.length; i++) {
                Thread worker = workers[i];
                try {
                    worker.join();
                } catch (InterruptedException ex) {
                    Logger.getLogger(MatrixMulti.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return matrixProduct;
        }
    
        private static void printMatrixToFile(int[][] matrix, String fileName) throws IOException {
            PrintWriter userOutput = new PrintWriter(new FileWriter(fileName));
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    userOutput.print(matrix[i][j] + " ");
                }
                userOutput.println();
            }
            userOutput.close();
    
        }
    
        private static void printBackMatrix(int[][] matrix) {
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    System.out.print(matrix[i][j] + " ");
                }
                System.out.println();
            }
        }
    
        private static void fillMatrix(int[][] matrix) {
            Random rand = new Random();
    
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    matrix[i][j] = rand.nextInt(100) + 1;
                }
            }
    
        }
    
        public static boolean validDimension(int dim) {
            if (dim <= 0 || dim > 1000) {
                System.err.println("Dimension value entered is not valid");
                return false;
            }
            return true;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms

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.