Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do threads created in Java behave differently on Windows and Linux?

As what I know, Java is using operating system threads (in contrast to i.e. Erlang), that means that the threads created with Java on Windows and Linux may behave different.

Are there any differences on Java threads on Windows and Linux? What is the biggest difference? It's maybe only a difference in performance?

like image 227
Jonas Avatar asked Jul 13 '10 13:07

Jonas


People also ask

Are Java threads native?

The standard threading model in Java, covering all JVM languages, uses native threads. This has been the case since Java 1.2 and is the case regardless of the underlying system that the JVM is running on. This means that any time we use any of the standard threading mechanisms in Java, then we're using native threads.

What is a thread How do threads behave in Java?

The Java thread lifecycle consists of six thread states: New: A new Thread() has been instantiated. Runnable: The Thread 's start() method has been invoked. Running: The start() method has been invoked and the thread is running. Suspended: The thread is temporarily suspended, and can be resumed by another thread.

Are JVM threads OS threads?

The OS simply views JVM as a single executing thread. The context switching and other intricacies of a green thread happens within the JVM only. These types of threads are very similar to the type of thread called user level thread of the OS because they exist within the user level of the underlying platform.


1 Answers

This is a very general question, so I'll give a general answer.

Java switched from green threads, to native threads early in its development. This does not mean that threads created on Windows and Linux will behave differently as both platforms will utilize native threads in their respective JVM implementations.

The thread interface exposed to Java by each OS, and similarly the native interfaces to threading via pthreads and Windows threads are very similar.

The biggest differences with respect to threading on the two platforms are that all threads on Linux are a form of process. Windows treats threads and processes very differently.

In my personal experience, native threads on Windows are slightly more lightweight and may perform slightly better within single process applications. Correspondingly (and perhaps irrelevant), are that Windows processes are extremely heavyweight compared with their Linux counterparts.

like image 189
Matt Joiner Avatar answered Sep 30 '22 00:09

Matt Joiner