by mstromb · 11y ago · 0 comments
Could you describe or at least point to some of them?
Instead of reaching out for ways to avoid blocking, we just make blocking free.
There is actually a library for java that adds it via byte code instrumentation (quasar or something) although not sure it will work for scala.
But saying that actor model is bad practice, I'm not sure that I accept it. Maybe on a single muticore but once you start talking distributed computing (e.g. Spark which is Akka based) then all this "avoid crossing to the kernel" optimization is becoming a drop in the ocean.
Here is an example of a small single threaded program beating out a number of distributed graph frameworks running on 128 cores, with a 128 billion edge dataset.
http://www.frankmcsherry.org/graph/scalability/cost/2015/02/...
Performance matters because it enables simplicity. If your language forces you to pull in multiple machines to solve your problem then its turned a simple program into a distributed system and life gets complicated fast. Just throwing more cores at a program without understanding why its slow will just get you into trouble.
Multithreaded programs and distributed programs should be a scary last resort after making absolutely sure you can't get away with the simple solution.
Yes, but in this context, they provide the same performance benefits as the asynchronous techniques mentioned in the article, without all the drawbacks of the cumbersome asynchronous style.
> But saying that actor model is bad practice
The actor model is a general technique for fault-tolerance, and it's great. How it handles concurrency, though, is an orthogonal concern. Akka has asynchronous (callback based) actors, and callbacks are an anti-pattern. Erlang has synchronous (blocking) actors, which are so much simpler, and don't have all the complications associated with asynchronous code.
"Free blocking" was what attracted me to Erlang's actors in green threads as opposed to Akka's actors that block entire threads when they block.
However, there is one thread you don't want to block: the UI thread. We do need ways of getting things to the UI asynchronously.
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:
uiLabel onMainThread setText:'num: ', i.
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.