区分 Java 线程和 OS 线程?

Distinguishing between Java threads and OS threads?(区分 Java 线程和 OS 线程?)

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

问题描述

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

How do I distinguish running Java threads and native threads?

在Linux中,每个子进程都会有一个父进程,他们说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 线程和 OS 线程有什么命名约定吗?

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线程是用native线程实现的,所以使用线程的Java程序与使用线程的native程序没有区别.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 Process)ID.主"线程的线程 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 系统可能使用不完全符合 POSIX 的linuxthreads"线程实现,而不是 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).

在操作系统级别,tads 没有名称;它们只存在于 JVM 中.

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

pthread_kill() C 函数可用于向特定线程发送信号,您可以使用该信号尝试从 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 线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:区分 Java 线程和 OS 线程?