区分Java线程和OS线程? [英] Distinguishing between Java threads and OS threads?

查看:102
本文介绍了区分Java线程和OS线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何区分正在运行的Java线程和本机线程?

How do I distinguish running Java threads and native threads?

在Linux中,每个子进程都会有Parent进程,他们说0是所有子进程的父进程在这个过程中,是否存在所有分叉Java线程的父线程?

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?

我如何知道哪个Java线程与OS线程相关(如果Java线程需要本机进程线程)。

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

是否存在Java线程和操作系统线程的命名约定?

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

可以运行吗? Java线程可以从另一个Java代码暂停或终止吗?

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

推荐答案

在Linux上,Java线程是用本机线程实现的,所以a使用线程的Java程序与使用线程的本机程序没有什么不同。 Java线程只是属于JVM进程的线程。

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.

在现代Linux系统(使用NPTL的系统)上,属于进程的所有线程都具有相同的进程ID和父进程ID,但不同的线程ID。您可以通过运行 ps -eLf 来查看这些ID。 PID列是进程ID,PPID列是父进程ID,LWP列是线程(LightWeight进程)ID。 main线程的线程ID与进程ID相同,其他线程将具有不同的线程ID值。

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.

较旧的Linux系统可能使用linuxthreads 线程实现,不完全符合POSIX,而不是NPTL。在linuxthreads系统上,线程具有不同的进程ID。

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.

您可以通过将系统的C库(libc)作为独立程序运行来检查您的系统是使用NPTL还是linuxthreads并查看其输出中的可用扩展。它应该提到Native POSIX Threads Library或linuxthreads。 C库的路径因系统而异:可能是 /lib/libc.so.6 /lib64/libc.so。 6 (在基于64位RedHat的系统上),或类似 /lib/x86_64-linux-gnu/libc.so.6 (在现代的基于Debian的系统上,例如Ubuntu)。

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

在操作系统级别,theads没有名字;那些只存在于JVM中。

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

pthread_kill() C函数可用于向a发送信号特定的线程,您可以使用它来尝试从JVM外部杀死该特定线程,但我不知道JVM将如何响应它。它可能只会杀死整个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.

这篇关于区分Java线程和OS线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆