4 ms·
The Java Synchronisers
- Freak_NL 10y agoConcurrency is one of the harder bits to learn to do properly, but the CountDownLatch always strikes me as one of the concurrency constructs that is intuitive to grasp. Because these are usually declared final in Java in order to use them in a Thread launched from another class, I just can't seem to resist putting this comment above their declaration: /* It's the */ final CountDownLatch latch = …
- koolba 10y agoGreat job! Now every time I use a CountDownLatch that song is going to be stuck in my head. ^/s!
- bluejekyll 10y agoConcurrency is hard, try to avoid anything that makes it harder. A lot of the functional languages have something we can all learn from here. For Java I recommend this: - always make your class fields final, if they must be non-final, question why and decide what will happen across threads. - use executors and futures instead of rolling your own threads. Most, if not all, concurrency patterns can be built around these. - when all else fails and you need the primitives from this article, make sure you read https://books.google.com/books/about/Java_Concurrency_in_Practice.html?id=EK43StEVfJIC https://books.google.com/books/about/Java_Concurrency_in_Pra... before writing a line of concurrent code. By the way, that book is all about Java, but is excellent at getting across concepts that a practical for general concurrency best practices.
- mobiuscog 10y agoUsing executors makes some thread 'issues' easier, but they shouldn't just be used as the default. They're great for task-based threads, but not as ideal for single-use threads that last the life of the application. There's also the issue that people just throw things into exectors without paying any attention to bounds (hey, just leave it unbounded) and resources. The book recommendation is solid though, and a great reference to keep re-reading. One of my desktop companions.
- ninjakeyboard 10y agoJava8 helps a lot - CompletableFutures can be easier to reason about IMO. If you need to share state, you can use Akka to encapsulate the state in an actor, and thus you will have (what appears to be) lock free concurrency!