简体   繁体   中英

How to pass data to Platform.runLater

I need to change the title of my GUI window based on the socket data received in the socket receiver thread. I want to know is there a way to pass variables like int , long etc. to Platform.runLater , in order to avoid mutex locks just for accessing an int variable. This is a sample code:

// Receiver Socket thread
int opnMode = byteBuffer.getInt();

Platform.runLater(() -> { //GUI thread
    ((Stage) Fx.Pane.getScene().getWindow()).setTitle("Operation mode " + " (" + opnMode + ")");
});

In the above code I'm using the variable opnMode without using mutex as it is being accessed by two threads.

Your code should work just fine as it is because your int is effectively final. Here is a discussion of this issue: https://www.baeldung.com/java-effectively-final

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM