What defines an “active” thread?

In Java concurrency, what makes a thread "active"? Just the fact that he is not single? Is the “pending” or “paused” stream still considered technically active?

+7
source share
5 answers

From what I can say, the term “active” seems to be used a lot, but not defined. The ThreadGroup.enumerate() method is documented:

Copies to the specified array each active thread in this thread of the group and its subgroups.

and looking at the source of this , it checks Thread.isAlive() and adding them to the enumerable. From this I conclude that the terms “active” and “live” are used interchangeably, and “live” is defined as:

A thread is alive if it has been started and has not yet died.

+5
source

In this context, I accept "active" to mean that they are executing code. Inactive threads — those that are blocked for I / O calls or wait for locks — consume only memory resources without affecting the CPU (or only slightly).

However, it really depends on what your threads are doing. If each thread iterates over numbers to calculate primes, they are completely processor bound, and in fact you should only have one per core to maximize throughput. If they make HTTP requests or perform file input / output, you can afford to have a few cells.

In the end, a generic statement covering all threads in general, no matter what they do, is pretty worthless.

I highly recommend the Java Concurrency book in practice for a high-quality treatment of the topic of parallel Java programming.

+1
source

Take a look at java.lang.Thread.State

On other non-Java systems, the active value is "RUNNABLE". Task / Process / Theme is active if it is able to actively run code. It is locked if not (lock, etc.).

As Stephen C said, active is used more like English than Java here.

+1
source

The word "active" is used here in the usual English sense.

The normal English meaning of the active word is "do something." Thus, the intuitive meaning of “active” threads is that they are actually running or are ready to run.

In the context of this quote:

"It is usually acceptable to have 4 to 20 active threads per core."

the word "active" is used sequentially with this, although I would play a trick on numbers. (20 pretty high, IMO).

However, I would say that this is a deliberately vague statement. The provision of aways is the use of the affectionate phrase "generally accepted" (instead of quoting any specific sources) and a wide range ("4-20"). Thus, you can make the word “active” another example of intentional uncertainty.

IMO, the author of the quote is trying to make a general conclusion that "too many threads are bad" ... not being exact with respect to the fact that "too many". Retrieving a more precise indication by linking what it means by “active” is pointless. (And to be clear, it's impossible to say how much “too much” ... in general.)

0
source

"It is usually acceptable to have 4 to 20 active threads per core." - Adam Tannon

In this sense, I think this word means "readiness to escape" and not "live" or any other possible meaning. It makes sense to have no more than a few threads in the finished queue, because you do not get any value for the waiting threads, and you contribute to the planning and the overhead of the context. On the other hand, if you have fewer threads ready to run than cores, you are not using the available resource.

0
source

All Articles