0

I have been reading that Thread scheduler in java runs only one thread at a time in a single process.

Lets say that we have one JVM running one single CPU machine. So if I start 10 threads they will be managed by the same Thread scheduler.

  1. If one thread is run at the time, how is that concurrent? Isn't this just an illusion of concurrency?

  2. Can I run 10 threads at the same in a single process under the same JVM?

Bex
  • 2,905
  • 2
  • 33
  • 36
Suleiman Alrosan
  • 337
  • 2
  • 4
  • 14

2 Answers2

3

1- If one thread run at the time, how is that concurrent? Isn't this just an illusion of concurrency?

It is both an illusion and real. Both tasks alternate with each other without your intervention. Thus, it is indeed concurrent and very useful for code organization. It would be an absolute nightmare to run multiple tasks at the same time without threading to perform the switching for you.

However, a single processor is never running multiple threads at any given moment because it can only perform a single operation at a time, and in that sense it is an illusion.

2- Can I run 10 threads at the same in a single process under the same JVM?

Yep. See https://stackoverflow.com/a/7727922/998251 .

Summary: You have several thousand threads available to you.

Community
  • 1
  • 1
Daniel Centore
  • 3,220
  • 1
  • 18
  • 39
  • Thanks that make sense. – Suleiman Alrosan Aug 17 '16 at 11:57
  • @SuleimanAlrosan, threads actually were being used in real-world programs back when multi-processor computers were still a laboratory curiosity. They were invented as a way to organize programs that have to wait for, and react to multiple, asynchronous sources of events. – Solomon Slow Aug 17 '16 at 13:27
  • I understand scheduling and asynchronous part, but my confusion is about how to make two threads run at the same given moment. And as my understand from @DanielCentroe this is not possible since a single processor can only perform one operation at a time. please correct me if Im wrong – Suleiman Alrosan Aug 18 '16 at 15:29
  • If you have more than one core then the two threads might be performing operations at the same exact moment. But one core can only be performing one operation at a moment. – Daniel Centore Aug 18 '16 at 15:41
  • However, two threads can be running at the same time. Their operations are just interleaved. They share processor time. So if you have one thread which repeatedly prints the number `1`, and one thread which prints the number `2`, the output might be something like `11212211211221122` – Daniel Centore Aug 18 '16 at 17:03
1

There are different things at work here. So a single cpu core can only run a single kernel thread at a time. How many kernel threads may be running for a single JVM process is dependent on the JVM. Even with a single kernel thread, though, having multiple Java threads can increase your concurrency. While each thread will have to timeslice it allows a non blocked thread to perform operations while another is blocked leading to an overall lower execution time. Imagine thread A makes a network call and is waiting on the result. Thread B could take over while A is waiting and perform some calculations. If it was only synchronous the calculations B performed couldn't start until the network call returned.

Aaron Davis
  • 1,731
  • 10
  • 13