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");
}
}
}
That! Only I put this in the run method and always prints the same ID
– Andreia Oliveira
I improved the answer then. currentThread only works to get the ID of the running thread.
– Rodrigo Guiotti
So I keep that code in the run method ?
– Andreia Oliveira
This is an option... maybe run once on each thread. But inside it would work.
– Rodrigo Guiotti
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
– Andreia Oliveira
It’s normal yes. Threads can have the same code if they’re from different processes as well.
– Rodrigo Guiotti
Ah,then that’s right. I’ll keep her in the run method
– Andreia Oliveira