Threads

2 respostas
M

Ola a todos.

Estou tentando usar um array de threads com o seguinte código:


MyThread[] thrs;

thrs = new MyThread[10];

for (i = 0; i < thrs.length(); i++){

thrs[i].setPriority(i);

thrs[i].start;

}

class MyThread extends Thread{

public void run(){



}

}

No entanto o compilador acusa NullPointerException na linha “thrs[i].setPriority(i);”

Eu não posso usar array de Thread?

Obrigado a todos.

mtolentino

2 Respostas

ozielneto


MyThread[] thrs;

thrs = new MyThread[10];

for (i = 0; i < thrs.length(); i++){

// insira esta linha

thrs[i] = new MyThread();
thrs[i].setPriority(i);

thrs[i].start;

}

class MyThread extends Thread{

public void run(){



}

}

Bom estudo…

maxguzenski

vc tem que dar um new em cada uma das posições do array…

MyThread[ 0 ] = new MyThread();

Criado 2 de junho de 2003
Ultima resposta 2 de jun. de 2003
Respostas 2
Participantes 3