i'm tryng to modify le list every time client send somethig.It have to work like a server log. I reserched for javafx concurrency and i found class Service and Task that are very usefully but when i try to modify the list on the second task, show an error of wrong thread "it's not on javafx thread". There is a way to resolve that? I'm studying now the concurrency and i can't understand the problem.Thanks.
Server: (I call a Connection conn on initialization of application with conn.start())
public class Connection extends Service<Void>{
Socket incoming;
ServerSocket s;
PrintWriter out;
@Override
protected Task<Void> createTask() {
return new Task<>() {
@Override
protected Void call() {
System.out.println("Server Attivo");
try {
s = new ServerSocket(8189);
while (!isCancelled()) {
System.out.println("In attesa di connessione....");
incoming = s.accept();
System.out.println("after accept "+Thread.currentThread());
server = new Task<Void>() {
@Override
protected Void call() throws Exception {
System.out.println("Connessione accettata!");
System.out.println(Thread.currentThread());
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
out = new PrintWriter(outStream, true);
Scanner in = new Scanner(inStream);String line;
out.println("Connessione accettata!");
while (!isCancelled()){
line = in.nextLine();
System.out.println(line);
list.add(new Log(line)); --> the error is here
}
return null;
}
};
new Thread(server).start();
}
}catch (SocketException ex){
System.out.println("Listener chiuso!");
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void cancelled() {
System.out.println("Closing server....");
System.out.println("Done!");
if(out!=null)out.println("Connessione chiusa!");
try {
s.close();
try {
incoming.close();
}catch (NullPointerException EX){
System.out.println("Non è stata stabilita nessuna connesione client prima della chiusura!");
}
}
catch (NullPointerException EX){
System.out.println("Server non inizializzato correttamente");
}
catch (IOException e) {
e.printStackTrace();
}
if(server!=null)server.cancel();
super.cancelled();
}
};
}
}
}