2 ms·
Concurrency 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 recomme
by bluejekyll 10y ago
Concurrency 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.