Strange characters when executing a method to add matrices

Asked

Viewed 63 times

0

Good morning guys, I’m a beginner in Java and I have a little problem here. Can anyone tell me what are these strange characters that appear on the console when executing a matrix sum method (I’m using Threads)?

inserir a descrição da imagem aqui

Method of Matrix Summation:

public class somaMatriz {

public int[][] matriz(int [][] m1, int [][] m2){
        int size = m1.length;
        int[][] c = new int[size][size];


        Runnable threadMatriz1 = new Runnable() {


            public void run() {
                    try {

                        for (int i=0; i<size; ++i){
                            for (int j=0; j<size; ++j){
                                // \t tem função de tabulação
                                // \n tem função de quebra linha
                                c[i][j] = m1[i][j] + m2[i][j];
                                System.out.printf("%d\t",c[i][j]);  
                            } 
                              System.out.printf("\n");
                        }   

                        Thread.sleep(1000); //Para demorar um segundo
                    } catch (InterruptedException e) {
                        System.out.println("A Thread sofreu uma interrupcao!");
                    }

            }
        };

        Thread th1 = new Thread(threadMatriz1);
        th1.start();

        return c; 

    }

}

Main to call the Matrix method:

package trabSD23;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int [][] ar1 = { {1,2,3}, {4,5,6}, {6,8,2} };
        int [][] ar2 = { {9,6,6}, {6,8,3}, {3,4,7} };
        int [][] ar3 = { {7,2,5}, {1,4,2}, {6,8,6} };   

        somaMatriz teste = new somaMatriz();

        for (int i = 0; i < 10; i++) {
            System.out.println(teste.matriz(ar1, ar2));

            System.out.println(teste.matriz(ar2, ar3));


        } 
    }

}

1 answer

0

The problem possibly occurs in Thread.

But I can’t even debug to see what happens.

I recommend removing this thread.

If you’re only using it to do with Thread.Sleep() you don’t need Runnable

  • Actually, I need to use Thread. I still can’t solve.

  • What’s the real reason to use thread?

  • It’s a college job, the professor asked to implement with Threads.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.