Print the Thread ID

Asked

Viewed 550 times

3

I have the following code and cannot print the Thread ID, only 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");
    }
}

}

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;
        }
    }

2

You are trying to access the ID in the array and not in the elements. To access the Ids individually you need to traverse the thread array 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 do the multidimensional thread array 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. The way it is, the first loop will wait for the completion of each thread before proceeding. The correct thing is to create all threads and call 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");
    }
}

0

currentThread() only works for the currently running thread.

Each Thread needs to run this part. Then, you can put something like this into the thread builder to capture the id and keep that code for when you need it. Or the function each thread uses to run take the ID and print it.

long threadId = Thread.currentThread().getId();
System.out.println("Thread ID:" + threadId);
  • That! Only I put this in the run method and always prints the same ID

  • I improved the answer then. currentThread only works to get the ID of the running thread.

  • So I keep that code in the run method ?

  • This is an option... maybe run once on each thread. But inside it would work.

  • OK, I put the snippet in the code in the run method,but it is normal not to change the ID ? I have already executed the code 5 times and the ID does not change

  • It’s normal yes. Threads can have the same code if they’re from different processes as well.

  • Ah,then that’s right. I’ll keep her in the run method

Show 2 more comments

Browser other questions tagged

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