Olá, estou com dois metodos em uma thread que nao estao sendo executados corretamente. Tenho a seguinte situação:
package core;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Teste extends Thread {
static Teste w;
BlockingQueue<Integer> listaTarefa;
public Teste() {
System.out.println("Teste Criado =>");
this.listaTarefa = new LinkedBlockingQueue<Integer>();
this.start();
}
public void run() {
System.out.println("\t Pronto para trabalhar ... \n");
while (true) {
try {
int tarefa = listaTarefa.take();
int result1 = calcNew(tarefa);
int result2 = calcOld(tarefa);
System.out.println("RESULTADO 1: " + Thread.currentThread().getName() + " " + result1);
System.out.println("RESULTADO 2: " + Thread.currentThread().getName() + " " + result2);
} catch (Exception e) {
}
}
}
public int calcNew(int x) {
return x * 2;
}
public int calcOld(int x) {
return x - 2;
}
public void addList(int valor) {
try {
this.listaTarefa.put(valor);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Quando instancio este objeto da seguinte e executo da seguinte forma:
package core;
public class ExecTeste {
/**
* @param args
*/
static Teste t1;
static Teste t2;
public static void main(String[] args) {
// TODO Auto-generated method stub
t1 = new Teste();
t2 = new Teste();
for (int i=0; i<10; i++) {
t1.addList(i*2);
t2.addList(i*4);
}
}
}
Geralmente os metodos das threads nao são executados. As vezes sim e as vezes somente os valores da t2 são informados. Muito estranhoo alguem consegue me auxiliar???
OBRIGADO