Live data from Hacker News

Java Concurrency – Understanding the Basics of Threads

turkogluc.com

31–40 of 86 posts

Re: Java Concurrency – Understanding the Basics of Threads

#31

With Futures and Executors/ExecutorServices I find that I rarely ever need to use raw Threads these days. Most of the thread-safety issues commonly encountered are eliminated with this approach as well.

I was going to comment that as well. Futures, Managed Executors, tasks, runnables, are more higher level structures that are better suited for general use. Those constructs are often implemented using threads though, so it's worth knowing what's happening one layer below the abstraction layer.

Re: Java Concurrency – Understanding the Basics of Threads

#33
post #16

Earlier quoted context omitted.

Idk about GP, but one book I highly recommend is Java Concurrency in Practice - https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz... It's old, but the material holds up well since it covers a lot of fundamentals

We were an all-Java shop and we were considering how to make our application a SAAS cloud application. Our senior engineers read this book. They all agreed that it was very educational, but the conclusion was that Java concurrency in practice has too many footguns, and so we ended up adopting Clojure. I think modern Java has better support for it, but if you've got mutable state spread throughout your application you…

> senior engineers read this book

How does one become a senior engineer if you don't understand concurrency?

Mutable state is most easily solved by having cpoies of everything, but then that's a tradeoff between performance and infrastructure/resource costs, but I guess that if you're in an all-Java shop that isn't much of an issue.

Re: Java Concurrency – Understanding the Basics of Threads

#34
post #24
post #17

Earlier quoted context omitted.

The problem for novices is that a program that behaves correctly looks a lot like a correct program. Until one day it doesn’t. 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.

If the resource is shared and being accessed from many threads and is both written to and read from then it is the correct behavior to to lock it with the proper type of lock at access time. Depending on resource it might be possible to split it into few with more granular access. As for novices: they are called that for reason and supposed to be under supervision rather than allowed running wild.

Why is this being downvoted? It's the truth.

HN needs to only allow downvotes that have an accompanying explanation comment.

Re: Java Concurrency – Understanding the Basics of Threads

#35
post #17
post #6

Earlier quoted context omitted.

The concepts of threads and concurrent data access is simple enough for any decent programmer to comprehend. There is no hell here. Sure there are some complex cases but complex cases will arise in many situations when programming things. 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 problem for novices is that a program that behaves correctly looks a lot like a correct program. Until one day it doesn’t. 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.

What's a better alternative to synchronizing access to shared resources?

Re: Java Concurrency – Understanding the Basics of Threads

#36

I'm not a Java developer, but isn't RxJava the current best practice around managing concurrency in Java? I thought the consensus was that manually dealing with thread creation is too error-prone and unmanagable.

RxJava has some nice tools for async buffering, debounce, etc., but it pays to understand CountdownLatch, Semaphore, Mutex, ExecutorService, etc., and I would definitely not consider RxJava a substitute for other things.

Do avoid anything related to the hoary old Java "Future" class, though. CompletionStage or get out!

Re: Java Concurrency – Understanding the Basics of Threads

#37

With Futures and Executors/ExecutorServices I find that I rarely ever need to use raw Threads these days. Most of the thread-safety issues commonly encountered are eliminated with this approach as well.

yeah, calling Thread.interrupt(), join() or similar methods are often a code smell for a bad "programming 101" teacher.

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.

Re: Java Concurrency – Understanding the Basics of Threads

#38

With Futures and Executors/ExecutorServices I find that I rarely ever need to use raw Threads these days. Most of the thread-safety issues commonly encountered are eliminated with this approach as well.

Absolutely, executors offer much better (safer, more consistent) lifecycle management of a Runnable vs. a homegrown solution in my experience. The last time I extended Thread I think it was just to pull off a custom name format.

Re: Java Concurrency – Understanding the Basics of Threads

#39
post #19

Years ago, I tried learning how to use threads by following tutorials similar to this one, where you are taught how to implement threads from {python, java, c++}. However, it wasn't until I studied operating systems (when I returned to graduate school for computer science) was I able to wrap my mind around threads — from a language agnostic view point, how and what lightweight processes are, how to implement locks an…

Seconded. It’s silly to learn threads “from the outside in” — thinking of them as an opaque abstraction and trying to understand the API they present. There’s no coherent abstraction there; you’ll only learn to cargo-cult the API, without gaining an intuition for what threads “are” or when and where you’d want to use those APIs. The key thing to know, is that threads aren’t a first-class kernel object. In OS kernels,…

This strikes me as rather focused on Linux kernel implementation details, since in Windows processes and threads are actually distinct concepts (as opposed to the Linux kernel, which really only knows about tasks), where every live process has n>1 threads and the address space of threads is afaik strictly defined through the process it is part of.

Re: Java Concurrency – Understanding the Basics of Threads

#40
What a strange coincidence! I have started learning Java concurrency from the book[1]. I am on the synchronization chapter and it looks like managing threads via Runnable directly is going to be painful. I am hoping I get a good intro to Eecutors somewhere down the road. Adding the OP tutorial to go through once I have a better hold on writing concurrent programs.

1. https://www.amazon.in/Java-Threads-Concurrency-Utilities-Fri...

Post reply on HN