Skip to content

Migration to Multi Thread

Pablo González edited this page Feb 24, 2020 · 4 revisions

To provide better performance LWJGUI has switched to a per-thread window code base. This is a small guide that takes you through the changes required to work with this new system.

General changes

Controls (Nodes) are not thread safe, they should be created and used in the same thread. window.runLater() or window.submitTask() can be used to run code in the window thread.

LWJGUIApplication based

Some changes may be required.

Code is now executed inside a ManagedThread.

The underlying class does not call System.exit() anymore, you should stop any custom thread and/or close other LWJGUI Windows before.

LWJGUIUtil or Custom code

For applications that use LWJGUIUtil or they own code for window creation the following changes are required.

WindowManager.init() Must be called in the main thread before any LWJGUI code but after glfwInit()


Window lwjguiWindow = LWJGUI.initialize(window);

Must be replaced with

Window lwjguiWindow = WindowManager.generateWindow(window);

The previous function is only provided to ease migration, it is a one-time main-thread only function used to generate a window from a handle. This functions may be removed in the future.

For any extra LWJGUI Window the ManagedThread class can be used. Check the PopupWindowExample or PopupWindowExampleExtends for information on how to use the class.

For any custom window where you want to use LWJGUI, the recommended way is to copy the source code of ManagedThread and modify it for your needs.


LWJGUI.render();

Must be replaced with

lwjguiWindow.render();
lwjguiWindow.updateDisplay(0);
WindowManager.update();

WindowManager.update() call must be always in the main thread loop regardless if there is a window in the main thread. It also calls glfwPollEvents().

You can use any sleep code in the loop, ClientSync class is available for such task.

Rendering is now done per-window, for that call lwjguiWindow.render()

lwjguiWindow.updateDisplay() calls glfwSwapBuffers() and if you pass a fps higher than 0 it will also throttle to that value. Due to this the auto swap property has been dropped, if you want to control the buffer swapping don't call updateDisplay().


WindowManager.dispose() Must be called in the main thread before glfwTerminate()

Clone this wiki locally