Print Thread ID

3

I have the following code and I can not print the ID of the Thread, just the size. How do I print the thread array ID?

public class MultiplicacaoMatrizesThread {
// Matriz A
public static int a[][] = { { 5, 4 }, { 3, 3 }, { 2, 4 } };
// Matriz B
public static int b[][] = { { 3, 2 }, { 8, 2 }, { 5, 8 } };

// Numero de threads
public static final int NUMERO_DE_THREAD = (a.length * b[0].length);
// Matriz resultante
public static int r[][] = new int[3][3];

public static void main(String[] args) throws InterruptedException {

    int indice_thread = 0;
    Thread[] thread = new Thread[NUMERO_DE_THREAD];
    for (int linha = 0; linha < a.length; linha++) {
        for (int coluna = 0; coluna < b[0].length; coluna++) {
            thread[indice_thread] = new Thread(new GeraMatrizThread(linha,
                    coluna, a, b, r));
            thread[indice_thread].start(); 
            thread[indice_thread].join();
            ++indice_thread;
        }
    }
    for (int linha = 0; linha < a.length; linha++) {
        for (int coluna = 0; coluna < b[0].length; coluna++) {
            System.out.print("[" + r[linha][coluna] + "] ");

            System.out.println(thread.length);
        }
        System.out.print("\n");
    }
}

}

    
asked by anonymous 15.03.2016 / 17:41

3 answers

3

If I understand correctly, so you can print the ID of the Thread array, try the following:

    int indice_thread = 0;
    Thread[] thread = new Thread[NUMERO_DE_THREAD];
    for (int linha = 0; linha < a.length; linha++) {
        for (int coluna = 0; coluna < b[0].length; coluna++) {
            thread[indice_thread] = new Thread();
            thread[indice_thread].start(); 
            thread[indice_thread].join();
            /** Imprime o Id da Thread que acabou de criar! **/
            System.out.println("ID: "+thread[indice_thread].getId());
            ++indice_thread;
        }
    }
    
15.03.2016 / 20:57
2

You are trying to access the ID in the array, not the elements. To access the IDs individually you need to traverse the array of threads in the second loop as you did in the first.

Example:

indice_thread = 0;
for (int linha = 0; linha < a.length; linha++) {
    for (int coluna = 0; coluna < b[0].length; coluna++) {
        System.out.print("[" + r[linha][coluna] + "] ");
        System.out.println(thread[indice_thread].getId());
        ++indice_thread;
    }
    System.out.print("\n");
}

However, you could make the multidimensional thread vector like the others and simplify the code like this:

public static void main(String[] args) throws InterruptedException {
    Thread[][] thread = new Thread[a.length][b[0].length];
    for (int linha = 0; linha < a.length; linha++) {
        for (int coluna = 0; coluna < b[0].length; coluna++) {
            Thread t = new Thread(new GeraMatrizThread(linha, coluna, a, b, r));
            t.start();
            t.join();
            thread[linha][coluna] = t;
        }
    }

    for (int linha = 0; linha < a.length; linha++) {
        for (int coluna = 0; coluna < b[0].length; coluna++) {
            System.out.print("[" + r[linha][coluna] + "] ");
            System.out.println(thread[linha][coluna].getId());
        }
        System.out.print("\n");
    }
}

Finally, the use of Thread#join is incorrect. As it stands, the first loop will wait for each thread to finish before proceeding. The correct thing is to create all the threads and call the join before collecting the results, as below:

public static void main(String[] args) throws InterruptedException {
    Thread[][] thread = new Thread[a.length][b[0].length];
    for (int linha = 0; linha < a.length; linha++) {
        for (int coluna = 0; coluna < b[0].length; coluna++) {
            Thread t = new Thread(new GeraMatrizThread(linha, coluna, a, b, r));
            t.start();
            thread[linha][coluna] = t;
        }
    }

    for (int linha = 0; linha < a.length; linha++) {
        for (int coluna = 0; coluna < b[0].length; coluna++) {
            thread[linha][coluna].join();
            System.out.print("[" + r[linha][coluna] + "] ");
            System.out.println(thread[linha][coluna].getId());
        }
        System.out.print("\n");
    }
}
    
16.03.2016 / 02:05
0

CurrentThread () only works for the currently running thread.

Each Thread needs to rotate this part. So, you can put something like this in the thread constructor to capture the id and keep this code for when you need it. Or the function that each thread uses to rotate to get the ID and print it.

long threadId = Thread.currentThread().getId();
System.out.println("Thread ID:" + threadId);
    
15.03.2016 / 17:53