当我运行程序时,它没有从服务器接收消息。它阻塞在o = in.readObject();
行中。我使用(在类线程中)在类Worker中创建的输入流。
@Override
public void run() {
System.out.println("run do worker começou.");
try {
//out = w.out;
//out.flush();
in = w.in;
String message = null;
Object o = null;
boolean identifier;
ArrayList<News> receivedNews;
System.out.println("vai começar while true do worker.");
while(true) {
System.out.println("object: "+o);
System.out.println("worker dentro do while.");
o = in.readObject();
System.out.println("object 2.0: "+o);
System.out.println("worker recebeu: "+o.toString());
if(o instanceof Boolean) {
identifier = (Boolean) o;
if(identifier == false) {
toWorker = true;
System.out.println("recebi o boolean");
}
}
if(o instanceof String && toWorker == true) { //recebe a palavra e guarda-a como atributo
message = (String) o;
word = message;
System.out.println("Worker received word: "+message);
toWorker = false;
}
if(o instanceof ArrayList && toWorker == true) { //recebe as notícias
receivedNews = (ArrayList<News>) o;
news = receivedNews;
System.out.println("Worker received the news");
newsFinal = searchNews();
w.sendListToServer(newsFinal);
toWorker = false;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
服务器代码:
out.writeObject(false); // false = enviar para worker
out.writeObject(word);
out.writeObject(news);