142 points · by turkogluc · 6y ago · 82 comments
They give you the tools to just say "go away and do these things", which after years of dealing directly with pthreads in C was a breath of fresh air!
which presents threads as the solution to the pain of using futures.
A future internally is just a value or a set of waiters. Whether they are "colored" (ie. can you wait on a future in a non-future context), or based on threads or cooperative concurrency (ie. async) etc. is entirely an implementation detail, with significantly differing trade-offs.
In that sense, you can really only compare future implementations with other concurrency systems.
Executors are the way to go for almost all finite-time concurrency.
New threads should normally only be used for stuff that keeps running until the process quits.
The key thing to know, is that threads aren’t a first-class kernel object. In OS kernels, there are only OS processes and memory regions.
To learn about threads, you should just learn about OS processes; and then learn that distinct OS processes can share memory regions between them, often via subprocess-spawn-time inheritance. Learn what fork(2) does on POSIX, and how it manages to be fast.
Starting with that intuition, it’s simple to then absorb what “threads” actually are: a usage pattern for spawning and managing OS processes that share memory; and a set of convenience APIs (that may be in-kernel, as in Windows; or purely in userland, as in Linux) for setting up this usage pattern. Everything these “threading” APIs can do, you can do yourself directly using the process-management and memory-mapping APIs. And those same calls are all that e.g. libpthread is doing.
Studying C, I haven't really come across threads other than trying out the things in `pthread.h`.
Would you recommend just reading the source code of that header for a better understanding?
It's old, but the material holds up well since it covers a lot of fundamentals
I think modern Java has better support for it, but if you've got mutable state spread throughout your application you're going to have a hard time no matter what.
That's really going to change and simplify JVM concurrency and I think many other things. Delimited continuations can be used to implement algebraic effects, which is exciting for functional programming.
How-so? I thought the type system was the precluding factor for algebraic effects, not the threading model.
The biggest challenge will be reconciling Futures, Netty and ThreadLocal (FiberLocal) patterns. Think defining a SQL transaction lifetime, a distributed lock, or a OpenTracing span. For Spring Framework people, no big deal. For everyone else, lots of complex decisions to make.
Do avoid anything related to the hoary old Java "Future" class, though. CompletionStage or get out!
RxJava feels like an unnatural port from single-threaded mindsets.
1. https://www.amazon.in/Java-Threads-Concurrency-Utilities-Fri...
Threads handled well do not need to have race conditions, and race conditions/deadlocks are also very possible in distributed, message-passing systems.
Can message passing accomplish this at the same level of performance?
although I agree that simplifying resource access should probably be considered before fully shared state.
Let me ask a different question: Why did databases take off in the way they did? Sure they persist stuff to disk, but so do files. What they offer is a concurrency model so good that you almost never think about it. Beginner programmers can competently write large, concurrent systems by writing single-threaded programs which are backed by a central DB, without even knowing the term "race condition".
If beginner database articles told users how to make database Threads, Thread groups, and how to signal and catch interruptions, I don't think databases would have enjoyed nearly as much popularity.
While Threads are fundamental to Java concurrency, I kinda agree with yetkin's point. It introduces the Thread footgun without even paying lip service to the problems of shared, mutable state.
I have been a long time Java developer, and I have worked a lot with highly concurrent code. Pony really opened my eyes to what was possible.
Unfortunately, the language, standard library and runtime is still pretty immature. It does however have very good 'C' interop. So for some problems it would be a very good fit.
And achieving concurrency without shared memory is impossible in general case. Sure it is possible to isolate such access to a separate layer and make it transparent for the rest of the program but someone still has to program such layer.
And because you’re in production and getting random spurious failures, the panicked (but common) reaction is to wrap every shared resource in a synchronized block. Which makes an incorrect implementation worse but possibly correct.
As for novices: they are called that for reason and supposed to be under supervision rather than allowed running wild.
It's notoriously difficult to reason about concurrent programs using intuition. Much more difficult than reasoning about non-concurrent imperative code. This is why there are articles like [0], and why a bug in a Wikipedia article on a fundamental concurrency algorithm went unnoticed until an analysis tool detected the issue, [1] and why lock-free algorithms in particular are so tricky to get right.
[0] https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedL...
if i had no idea about C10K problem, success of Nginx, Redis, other concurrency success stories on Actors, CSP concept, Concurrency via messages over shared memory, I would say threads are ok when you can use it. But indeed it is so simple and tempting people to design shitty software. Software is a very welcoming medium for it. it is hell.