Olá tenho o seguinte código abaixo, não estou no computador que possui o código por completo, mas estou enviando minha dúvida sobre o que realmente interessa:
public class Trabalhador extends Thread {
GResposta gresposta;
BlockingDeque<Trabalho> list;
Trabalho trabalho;
String trabalhadorID;
public Trabalhador(String id, GResposta grepsosta) {
try {
this.trabalhadorID = id;
this.gresposta = gresposta;
this.list = new LinkedBlockingDeque<Trabalho>();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
while (true) {
this.trabalho = this.list.take();
this.trabalho.resolver();
this.grespostta.resposta(this.trabalhadorID(), this.trabalho.getID(), this.trabalho.getResposta));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void novoTrabalho(Trabalho t) {
// TODO Auto-generated method stub
try {
this.list.offer(t);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class GTrabalho extends Thread {
BlockingDeque<Requisicao> requisicaoList;
List<Trabalho> list;
Map<String, Trabalho> sendList;
Trabalhador trabalhador;
String GTrabalhoId;
public GTrabalho(String id, Trabalhador trabalhador) {
this.GTrabalhoId = id;
this.trabalhadir = trabalhador
this.requisicaoList = new LinkedBlockingDeque<Requisicao>();
this.list = new LinkedList<Trabalho>();
this.sendList = new LinkedHashMap<String, Trabalho>();
}
public void run() {
try {
while (treu) {
this.requisicao = this.requisicaoList.take();
this.t = gereTrabalho(this.requisicao);
this.trabalhador.novoTrabalho(this.t);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setSendJobList(String trabalhoId, Trabalho t) {
try {
this.sendJobsList.put(trabalhoId, t);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public IJob getSendList(String trabalhoID){
try {
return this.sendList.remove(trabalhoID);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void novaRequisicao(Requisicao r) {
// TODO Auto-generated method stub
try {
this.requisicaoList.offer(r);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class GResposta extends Thread {
GTrabalho gtrabalho;
BlockingDeque<Trabalho> respostaList;
Trabalho trabalho, t;
String GRepostaId;
public GResposta(String id, GTrabalho gtrabalho) {
this.GRespostaId = id;
this.gtrabalho = gtrabalho;
this.respostaList = new LinkedBlockingDeque<Trabalho>();
}
public void run () {
try {
while(this.running) {
this.t = this.respostaList.take();
this.trabalho = this.gtrabalho.getSendList(this.t.getID());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void resposta(String trabalhadorId, String trabalhoId, int resposta) {
// TODO Auto-generated method stub
try {
Trabalho trabalho = new Trabalho();
trabalho.setID(trabalhoId);
trabalho.setResposta(resposta);
trabalho.setTrabalhador(trabalhadorId);
this.answerList.offer(trabalho);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Minha dúvida é se essas listas sendList e o método GResposta.resposta devem ser sincronizados. E como eu deveria implementa-los?
Desde de já agradeço a ajuda.