Java threads on linux

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?

Stephen C, I’m learning how Linux Thread naming and Java thread Naming are inter related. And as well, How JVM Thread management is managed within Linux.

4 Answers 4

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.

Читайте также:  Вызов командной строки astra linux

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.

Источник

Real Time Java threads and OS Level threads on Linux

When using real time java threads (either RealtimeThread or NoHeapRealtimeThread ), is there a 1 to 1 relationship between OS Level threads and Java threads? Also, is Java using fork() or clone() for each of the processes created at the OS Level?

3 Answers 3

Java thread on linux depends on the version, but most modern implementations use pthread, the thread for linux, not really a process. the linux thread is also know as lightweight processes, which is not generated by an fork call, but rather the pthread call. Threads run under the same process, and can share certain resources.

Читайте также:  Установить тор браузер kali linux

Yes they are of 1 to 1 relationship, (ps -Lf), but it is really hard to find out which is which, as the os thread id is an magic number only the jvm knows.

The article below should help.

is Java using fork() or clone() for each of the processes created at the OS Level?

If you mean processes created by Runtime.exec(), it must use fork(). If you are still referring to threads it cannot use fork(), as threads are not processes.

From what I have seen on a RedHat 3.x — 5.x with Sun/Oracle JVM, It’s a one OS process per Java thread. Don’t know about fork vs. clone though.

I think you might be confused by what ps is telling you. It is certainly NOT one «process» per thread because processes don’t share address spaces. (And no . Java doesn’t use shared memory to do this either.)

some people have called threads light-weight processes, but that is to distinguish them from regular (heavy-weight) processes. The correct (1) term is OS or native thread . and if you start calling them processes, people will get confused. (1 Correct == what the OS documentation calls them . )

Источник

How to find a Java thread running on Linux with ps -axl?

I have a running JVM with two threads. Is it possible to see these running threads on my Linux OS with ps -axl ? I am trying to find out what priority the OS is giving to my threads. More info about this other issue here.

8 Answers 8

for finding your java process. Sample Output:

3825 RemoteMavenServer -Djava.awt.headless=true -Xmx512m -Dfile.encoding=MacRoman 6172 AppMain -Didea.launcher.port=7533 -Didea.launcher.bin.path=/Applications/IntelliJ IDEA 10.app/bin -Dfile.encoding=UTF-8 6175 Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/1.6.0_31-b04-411.jdk/Contents/Home -Xms8m 

(6172 is id of your process) to get stack of threads inside jvm. Thread priority could be found from it. Sample output:

. "main" **prio=5** tid=7ff255800800 nid=0x104bec000 waiting on condition [104beb000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at au.com.byr.Sample.main(Sample.java:11) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) . 

EDIT: If application running under different user than yourself (typical case on production and other non-local environments) then jps/jstack should be run via sudo. Examples:

sudo jps -v sudo jstack 6172 

Источник

Оцените статью
Adblock
detector