Distinguishing between Java threads and OS threads?

29,077

Solution 1

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.

Solution 2

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).

Solution 3

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

In theory yes. In practice, the Thread.kill() and Thread.suspend() methods are deprecated because they are unsafe except in very limited situations. The basic problem is that killing or suspending a Java thread is likely to mess up other threads that depend on it, and shared data structures that it might have been in the middle of updating.

If "another Java code" is meant to mean another JVM, then the chances of it working are even less. Even if you figured out how to send the relevant thread signal, the results are completely unpredictable. My bet is that the "target" JVM would crash.

Solution 4

There is no generic solution how Java threads are mapped to OS threads, if at all. Every JVM implementation can do it in a different way.

There is also a pure Java thread implementation, called green threads. This is used as a fallback if native threads aren't supported or the system isn't multithreaded at all. You won't see any green threads at your OS.

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

If they are running at the same JVM, yes, with stop(). But that's not a good solution and may work, or not. interrupt() allows the thread so safely shut itself down.

There is no way to kill threads outside of the JVM I know of. If a OS really supports killing of threads, I wouldn't expect the Java application to run correctly afterwards!

Share:
29,077
karthi
Author by

karthi

Updated on July 09, 2022

Comments

  • karthi
    karthi almost 2 years

    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?

  • karthi
    karthi over 14 years
    Thanks Wyard, Can I name running thread and Query details of runing thread ? I'm asking from Debugging perspective.
  • Wyzard
    Wyzard over 14 years
    A thread's "name" is Java-specific; to see that sort of information you'd have to connect to the JVM with a debugger. From outside the JVM you can only see its numeric ID, which is its only identifier from a POSIX standpoint.
  • karthi
    karthi over 14 years
    What one should do to Display Java thread name in Shell prompt? If I attach my profiler or Debugger to a running JVM, will I be able to get the running Java thread names and supply to Shell script ?
  • Igor Čordaš
    Igor Čordaš almost 10 years
    You could just execute Linux command from your Java code to kill a specific thread running under certain JVM. You can even do this on Windows. But I guess you are probably right that will just crash the parenting JVM of that thread. If you control both programs you should probably develop some kind of IPC calls to tell the threads in another process to stop gratuitously.
  • Vüsal
    Vüsal about 5 years
    "which you could use to try to kill that specific thread from outside the JVM" - You can use pthread_kill to send a signal only to a thread running on the same context as your signal sender. From man pthread_kill(3) "The pthread_kill() function sends the signal sig to thread, a thread in the same process as the caller.."