1

This is what I see in Oracle documentation and would like to confirm my understanding (source):

A computer system normally has many active processes and threads. This is true even in systems that only have a single execution core, and thus only have one thread actually executing at any given moment. Processing time for a single core is shared among processes and threads through an OS feature called time slicing.

Does it mean that in a single core machine only one thread can be executed at given moment? And, does it mean that on multi core machine multiple threads can be executed at given moment?

6
  • Can you please fix your quotations? Commented Sep 9, 2018 at 10:41
  • Quotations still broken, we don't know what Oracle doc you are referring to Commented Sep 9, 2018 at 10:58
  • docs.oracle.com/javase/tutorial/essential/concurrency/… Commented Sep 9, 2018 at 11:04
  • Which part did you want to quote? I can edit your post for you if you want. Commented Sep 9, 2018 at 11:05
  • Yes please, this part - " This is true even in systems that only have a single execution core, and thus only have one thread actually executing at any given moment. Processing time for a single core is shared among processes and threads through an OS feature called time slicin" Commented Sep 9, 2018 at 11:08

2 Answers 2

3

one thread actually executing at any given moment

Imagine that this is game where 10 people try to sit on 9 chairs in a circle (I think you might know the game) - there isn't enough chairs for every one, but the entire group of people is moving, always. It's just that everyone sits on the chair for some amount of time (very simplified version of time slicing).

Thus multiple processes can run on the same core.

But even if you have multiple processors, it does not mean that a certain thread will run only on that processor during it's entire lifetime. There are tools to achieve that (even in java) and it's called thread affinity, where you would pin a thread only to some processor (this is quite handy in some situations). That thread can be moved (scheduled by the OS) to run on a different core, while running, this is called context switching and for some applications this switching to a different CPU is sometimes un-wanted.

At the same time, of course, multiple threads can run in parallel on different cores.

Sign up to request clarification or add additional context in comments.

Comments

1

Does it mean that in a single core machine only one thread can be executed at given moment?

Nope, you can easily have more threads than processors assuming they're not doing CPU-bound work. For example, if you have two threads mostly waiting on IO (either from network or local storage) and another thread consuming the data fetched by the first two threads, you could certainly run that on a machine with a single core and obtain better performance than with a single thread.

And, does it mean that on multi core machine multiple threads can be executed at given moment?

Well yeah you can execute any number of threads on any number of cores, provided that you have enough memory to allocate a stack for each of them. Obviously if each thread makes intensive use of the CPU it will stop being efficient when the number of threads exceeds the number of cores.

3 Comments

I understand that I can I have more threads, but, from execution point of view, only one thread is executed at a time in single core machine?
Well if you go down at the CPU level only one instruction is executed at a time on a single core and many processes (and within those processes, potentially many threads) alternatively get to run a few instructions on the processor.
only one instruction is executed at a time on a single core”—in a very simplistic view. Actually, all modern CPUs (considering the last two or almost three decades as modern) have pipeline stages and multiple execution units in one core, actually processing multiple instructions at once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.