2

我正在实现一个通过 wifi (android) 与手机通信的 JavaFX 应用程序。

因此,我在后台进程中运行的 JavaFX 上有一个服务器线程:

public class NetworkService implements Runnable { 

    private final ServerSocket serverSocket;
    private final ExecutorService pool;

    private RoutePlannerJFX application;
    private UserData userData;


    public NetworkService(ExecutorService pool,
                          ServerSocket serverSocket,
                          RoutePlannerJFX app,
                          UserData data) {
        this.serverSocket = serverSocket;
        this.pool = pool;
        application = app;
        userData = data;
    }


    public void run() {
        try {
            while ( true ) {
                Socket cs = serverSocket.accept(); 
                pool.execute(new Handler(serverSocket, cs, application, userData));
            }
        } catch (IOException ex) {
            System.out.println("--- Interrupt NetworkService-run");
        }
        finally {
            System.out.println("--- Ende NetworkService(pool.shutdown)");
            pool.shutdown();  //keine Annahme von neuen Anforderungen
            try {
                pool.awaitTermination(4L, TimeUnit.SECONDS);
                if ( !serverSocket.isClosed() ) {
                    System.out.println("--- Ende NetworkService:ServerSocket close");
                    serverSocket.close();
                }
            } catch ( IOException e ) { }
            catch ( InterruptedException ei ) { }
        }
    }
}

它有一个处理程序:

public class Handler implements Runnable {

    private final Socket client;
    private final ServerSocket serverSocket;

    private RoutePlannerJFX application;
    private UserData userData;


    Handler(ServerSocket serverSocket,Socket client, RoutePlannerJFX app, UserData data) {
        this.client = client;
        this.serverSocket = serverSocket;
        application = app;
        userData = data;
    }


    public void run() {
        StringBuffer sb = new StringBuffer();
        PrintWriter out = null;
        try {
            System.out.println( "running service, " + Thread.currentThread() );
            out = new PrintWriter( client.getOutputStream(), true );
            BufferedReader bufferedReader = 
                new BufferedReader(
                    new InputStreamReader(
                        client.getInputStream()));
            char[] buffer = new char[100];
            int anzahlZeichen = bufferedReader.read(buffer, 0, 100); 
            String nachricht = new String(buffer, 0, anzahlZeichen);
            String[] werte = nachricht.split("\\s");


            System.out.println(nachricht+"\n");

            POI poi = new POI(nachricht);
            userData.addItemToPoiList(poi);

            application.setScene("INSTRUCT");

        } catch (IOException e) {System.out.println("IOException, Handler-run");}
        finally { 
            System.out.println(sb);  //Rückgabe Ergebnis an den Client
            if ( !client.isClosed() ) {
            System.out.println("****** Handler:Client close");
            try {
                client.close();
                } catch ( IOException e ) { }
            } 
        }
    }
}

该应用程序具有更改场景的公共方法(setScene())。

这就是我希望我能做到的方式,但现在我知道,我无法在后台进程中切换场景。

有谁知道如何解决这个问题?当我的后台进程收到来自客户端的消息时,我需要触发一个操作,但我不知道最好的方法是什么……我已经找到了 javafx.concurrent,但是我必须使用哪个以及如何使用?

提前致谢!

4

1 回答 1

2

对于客户端,您可以使用任务或服务在单独的线程中运行并更新 GUI,如文档中所述

你也可以查看这篇文章JavaFX Async Task

于 2012-09-03T22:34:57.433 回答