0

Say I have singleThreadExecutor and I 'give' it 10 tasks. Those tasks will be stored in BlockingQueue and they will be executed one after another. So in Java we have 1 thread that will do task by task, until it does all 10.

My question is does that mean that given Java thread will be run on same CPU core all this time? Is there any promise in this situation?

One more thing. Could someone tell me does this have something to do that 1 Java thread is not equal to 1 CPU thread? Did I say it correctly? I can't find appropriate terminology for this, so any reference for this last thing could help :) I just remember I read something like this in the past..

Stefan
  • 848
  • 2
  • 7
  • 1
    Multithreading in java is simply said a way to do paralel computations logical wise. If we are talking about CPU core usage etc., there is simply no way to guarantee excecution order or core usage. One java Thread might even be excecuted on multiple CPU cores. That is up to the CPU to decide and so unpredictable, that it is better to see it as a magical black box instead of something you can influence with programming habbits/patterns. – n247s Sep 24 '20 at 04:18
  • 1
    of course Java thread is not equal to 1 CPU thread. CPU thread is a hardware processor. Java thread is a stack of procedure invocations. – Alexei Kaigorodov Sep 24 '20 at 09:33
  • 1
    Too much noise in this question. When you execute *one* task on *one* thread, there is no guaranty that the task will run on the same CPU core. If you didn’t know that, you could have started asking about precisely that. Then, this doesn’t change when you execute ten tasks on that thread instead of one and it doesn’t change when you abstract/encapsulate the thread with an executor service. Neither does the `BlockingQueue` affect anything about that. – Holger Sep 24 '20 at 16:18
  • And BTW nobody guarantees that all your 10 tasks will be executed within the very same thread. Thread pool recreates thread when it dies during execution of the task: https://stackoverflow.com/questions/37909543/are-dead-threads-replaced-in-an-executioncontext-and-or-java-thread-pool – Ivan Sep 28 '20 at 00:45

0 Answers0