0

I am trying to understand the multi-threading concepts. Does the support of multi-threading comes from:

1) From Operating system? (OR)
2) Language itself? (like Java if I am correct)

What is the role of CPU, does multi-threading capability is also due to CPU (not considering the multi-core processors)?

Can there be a scenario where OS or CPU isn't supporting the multi-threading but still possible with language itself?

Can anyone help me understand this?

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 1
    Your question is very broad and difficult to answer. Are you asking about the real world or things that are possible in theory? – Oleg Sep 05 '17 at 16:50
  • 1
    Java process runs under a JRE as a OS process and within this there can be multiple threads. Since Java is platform independent the exact implementation varies for JRE implementation. For example there are new non blocking algorithms that can utilize modern Hardware and improve performance. – Amit Mahajan Sep 05 '17 at 16:50
  • I am trying to understand at a higher level what role of OS, CPU and language itself has for multi-threading programming; of course, not at in-depth level. – CuriousMind Sep 05 '17 at 16:52

2 Answers2

1

A thread is a sequence of instructions that can be managed independently from other such sequences by a scheduler.

Usually, the scheduler is part of the operating system (for example, Linux's Completely Fair Scheduler).

In some approaches (for example, green threads, stackless Python), the scheduler is part of the language or the runtime environment.

In modern computing environments it is usually the case that the number of threads exceeds the number of CPU cores. This is usually handled through time slicing, when threads take turns running on the available hardware. It is the job of the scheduler to manage that.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Understanding java's native threads and the jvm has some information.

Basically, multithreading support comes from both the OS and Java in Java's case. This does not happen for, for instance, Python (the standard CPython has just a wrapper around Linux's threads).

https://superuser.com/questions/740611/what-is-the-difference-between-multithreading-and-hyperthreading details what a CPU does in order to multithread.

In theory, yes, a language can be the implementor of the threading. Depending on how you look at it, C doesn't count on the OS, it does it's own threading (mostly because the OS is written in C). The above link also says this.

The language doing its own threading may not be as efficient as OS-level threading, so OS-threading is preferred and commonly present.

Heman Gandhi
  • 1,343
  • 9
  • 12
  • `C doesn't count on the OS` - If `C` here is used to implement a firmware or an OS, for example, then that's true. In this case, it may however, depend on Assembly to interact with the (micro-)processor and implement the concept of threading (normally a scheduler implemented in a kernel). In other cases, the application written in C _most likely_ depends on the OS to use the multi-threading support - For example, an application running on Windows/Linux/maOS. – jweyrich Sep 05 '17 at 16:59