Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguishing between Java threads and OS threads?

How do I distinguish running Java threads and native threads?

In Linux there will be Parent process for every child process, and they say 0 is the parent of all the process, will there be a Parent thread of all the forked Java threads?

How do I know which Java thread is related to OS thread (if a Java thread forkes a native process thread).

Is there any naming convention of Java threads and OS threads?

Can a running Java threads can be suspended or killed from another Java code?

like image 269
karthi Avatar asked Dec 11 '09 13:12

karthi


People also ask

Are JVM threads OS threads?

This is not true. Thus not specified, we often see, that Java threads are in fact native OS threads and that multithreaded Java applications really make use of multi-core processors or multi-processor platforms.

What is the difference between CPU thread and OS thread?

So the other CPU thread can only run if the first CPU thread is stalled (e.g. because of some cache miss). So practically having two CPU threads won't double the performance. There is an OS that does not support threads.

What is a OS thread?

A thread is the smallest unit of processing that can be performed in an OS. In most modern operating systems, a thread exists within a process - that is, a single process may contain multiple threads.

What are the two types of thread in OS?

In the operating system, there are two types of threads. Kernel level thread. User-level thread.


2 Answers

On Linux, Java threads are implemented with native threads, so a Java program using threads is no different from a native program using threads. A "Java thread" is just a thread belonging to a JVM process.

On a modern Linux system (one using NPTL), all threads belonging to a process have the same process ID and parent process ID, but different thread IDs. You can see these IDs by running ps -eLf. The PID column is the process ID, the PPID column is the parent process ID, and the LWP column is the thread (LightWeight Process) ID. The "main" thread has a thread ID that's the same as the process ID, and additional threads will have different thread ID values.

Older Linux systems may use the "linuxthreads" threading implementation, which is not fully POSIX-compliant, instead of NPTL. On a linuxthreads system, threads have different process IDs.

You can check whether your system is using NPTL or linuxthreads by running the system's C library (libc) as a standalone program and looking under "Available extensions" in its output. It should mention either "Native POSIX Threads Library" or linuxthreads. The path to the C library varies from system to system: it may be /lib/libc.so.6, /lib64/libc.so.6 (on 64-bit RedHat-based systems), or something like /lib/x86_64-linux-gnu/libc.so.6 (on modern Debian-based systems such as Ubuntu).

At the OS level, theads don't have names; those exist only within the JVM.

The pthread_kill() C function can be used to send a signal to a specific thread, which you could use to try to kill that specific thread from outside the JVM, but I don't know how the JVM would respond to it. It might just kill the whole JVM.

like image 130
Wyzard Avatar answered Oct 08 '22 13:10

Wyzard


There is no standard; this completely depends on the Java implementation which you're using. Also, don't mix up "native threads" and "native processes". A process is an isolated entity which can't see into the address space of other processes. A thread is something which runs in the address space of a native process and which can see into the memory of other threads of the same process.

What you see on Linux is something else: Some versions of Linux create an entry in the process table for each thread of a parent process. These "processes" aren't real processes (in the isolation sense). They are threads which can be listed with the ps command. You can find the process which created them by using the parent PID (PPID).

like image 40
Aaron Digulla Avatar answered Oct 08 '22 12:10

Aaron Digulla