Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doesn't the fact that Go and Java use User space thread mean that you can't really take advantage of multiple core?

We've been talking about threads in my operating system class a lot lately and one question has come to my mind.

Since Go, (and Java) uses User-space thread instead of kernel threads, doesn't that mean that you can't effectively take advantages of multiple cores since the OS only allocates CPU time to the process and not the threads themselves?

This seems to confirm the fact that you can't

Wikipedia also seems to think so

like image 908
Gab Royer Avatar asked Nov 16 '09 02:11

Gab Royer


People also ask

Does Java use multiple cores?

Java will benefit from multiple cores, if the OS distribute threads over the available processors. JVM itself do not do anything special to get its threads scheduled evenly across multiple cores.

How many threads should I use Java?

One thread per processor/core will maximize processing power and minimize context switching.

How many threads can concurrently Java?

Each JVM server can have a maximum of 256 threads to run Java applications. In a CICS region you can have a maximum of 2000 threads. If you have many JVM servers running in the CICS region (for example, more than seven), you cannot set the maximum value for every JVM server.


2 Answers

What makes you think Go uses User-space threads?

It doesn't. It uses OS-threads and can take advantage of multiple cores.

You might be puzzled by the fact that by default Go only uses 1 thread to run your program. If you start two goroutines they run in one thread. But if one goroutine blocks for I/O Go creates a second thread and continues to run the other goroutine on the new thread.

If you really want to unlock the full multi-core power just use the GOMAXPROCS() function.

runtime.GOMAXPROCS(4); //somewhere in main

Now your program would use 4 OS-threads (instead of 1) and would be able to fully use a e.g. 4 core system.

like image 166
jitter Avatar answered Oct 12 '22 09:10

jitter


Most recent versions of Java to use OS threads, although there is not necessarily a one-to-one mapping with Java threads. Java clearly does work quite nicely across many hardware threads.

like image 34
Tom Hawtin - tackline Avatar answered Oct 12 '22 10:10

Tom Hawtin - tackline