-2

After reading about concurrency with Java threads, I got a bit confused. Some have claimed (they might be wrong) that Java threads are executed concurrently?

If you have 4 CPU's that can do multithreading (can handle 8 threads), how is it possible that when you create 30 threads in your Java code that these are all executed concurrently (at the same time)? Only 4 can be run concurrently so far I know and the other 4 threads are waiting for execution and 22 are in the queue pool. Am I missing something or am I understanding the term concurrently wrong?

John
  • 71
  • 3
  • 3
    "Concurrently" does not mean that all are running at the same instant. It only means that all have _started_ before any of them have stopped. The operating system switches each processor from thread-to-thread-to-thread at a high enough rate to create the illusion that they all are running in parallel. – Solomon Slow Jan 18 '19 at 15:05
  • *Some have claimed (they might be wrong) that Java threads are executed concurrently* Thats the point of threads isn't it? – Antoniossss Jan 18 '19 at 15:08
  • If a given program has more threads than your computer platform can physically handle, threads start to compete for available resources, simple as that. Each thread will be given some amount of execution time by the virtual machine and/or the operating system, and will be paused and waiting otherwise. You can affect this to a degree by assigning a priority to a thread, but ultimately this is out of your control, and in control of an operating system (more so) and VMs (less so). – M. Prokhorov Jan 18 '19 at 15:11
  • If your CPU can handle 8 threads, then 8 threads can run in parallel (actually making progress at the same time), but any number of threads can be run concurrently (being continuously scheduled in and out by the OS). – Marco Jan 18 '19 at 15:12
  • 3
    I'm writing this and watching TV concurrently :-) Actually, my attention is shifting back and forth. –  Jan 19 '19 at 02:13

2 Answers2

0

Its simple - in your scenario 30 threads are started but only 4 of them are executed at the same time (at given instant). Threads are executed litle piece by piece. Pieces from any thread can be executed but only 4 at given time.

If you would have 2 construction teams (Cores) that would built 10 houses (Threads) at once, but every house would be build only for 4 hours a day by single team (thread scheduling), you would still say that 10 houses are built concurrently, but not necessarily at exact same time (as only 2 of them will be build at exact same moment).

The same happens with threads in OS and JVM.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

All the threads you have will run concurrently, as long as they are not blocked waiting for each other or waiting on some I/O. They will not wait for each other to terminate (unless you program it that way through bounded sized thread pools or completable futures).

Now the level and performance of concurrency will be determined by the hardware you have (the more cores the better) and the underlying operating system's thread scheduling mechanism.

Remember that in parallel to your program, your operating system will also have other processes (each taking cpu time too). The mechanism of concurrency between processes and threads is similar (as long as they are not user level threads). The operating system will do context switching at the opportune times. You shouldn't notice this in your program (unless the other processes are really cpu intensive).

jbx
  • 21,365
  • 18
  • 90
  • 144