|
|
|
|
|
by pron
4100 days ago
|
|
> And in fact we can even use kernel threads most of the time. Well, not if you need tens-of-thousands of them or more. > However, there is one thread you don't want to block: the UI thread. No problem. You simply multiplex fibers onto the UI thread, where they can block all they want. Here's an example in Quasar (Java): FiberScheduler uiScheduler = new FiberExecutorScheduler("UI-scheduler",
task -> EventQueue.invokeLater(task)); // schedule on the UI thread
new Fiber(uiScheduler, () -> {
for (i = 0;; i++) {
assert EventQueue.isDispatchThread(); // I'm on the UI thread!
uiLabel.setText("num: " + i); // I can update the ui
Fiber.sleep(1000); // ... yet I can block all I want
}
}).start();
|
|
Yes, I can easily dispatch something onto the UI thread using the technique you describe, but for that using a simple HOM is much more convenient:
Neither this nor your example are actually blocking the UI thread, because they are really just incidentally there and just pushing data in, a quick in/out. (And I assume that "Fiber.sleep()" takes the Fiber off the main thread )However, the more difficult part is if the UI thread actually has control flow and data dependencies, let's say a table view that is requesting data lazily. Taking the blocking computation off the UI thread doesn't work there, because the return value is needed by UI thread to continue.