简体   繁体   English

区分 Java 线程和 OS 线程?

[英]Distinguishing between Java threads and OS threads?

How do I distinguish running Java threads and native threads?如何区分运行的 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?在Linux中,每个子进程都会有一个父进程,他们说0是所有进程的父进程,所有分叉的Java线程都会有一个父线程吗?

How do I know which Java thread is related to OS thread (if a Java thread forkes a native process thread).我如何知道哪个 Java 线程与 OS 线程相关(如果 Java 线程派生出本机进程线程)。

Is there any naming convention of Java threads and OS threads? Java 线程和 OS 线程是否有任何命名约定?

Can a running Java threads can be suspended or killed from another Java code?一个正在运行的 Java 线程可以被另一个 Java 代码挂起或杀死吗?

On Linux, Java threads are implemented with native threads, so a Java program using threads is no different from a native program using threads.在 Linux 上,Java 线程是通过原生线程实现的,因此使用线程的 Java 程序与使用线程的原生程序没有区别。 A "Java thread" is just a thread belonging to a JVM process. “Java 线程”只是属于 JVM 进程的线程。

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.在现代 Linux 系统(使用 NPTL 的系统)上,属于一个进程的所有线程具有相同的进程 ID 和父进程 ID,但线程 ID 不同。 You can see these IDs by running ps -eLf .您可以通过运行ps -eLf来查看这些 ID。 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. PID列是进程ID,PPID列是父进程ID,LWP列是线程(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. “主”线程的线程 ID 与进程 ID 相同,其他线程将具有不同的线程 ID 值。

Older Linux systems may use the "linuxthreads" threading implementation, which is not fully POSIX-compliant, instead of NPTL.较旧的 Linux 系统可能使用不完全符合 POSIX 的“linuxthreads”线程实现,而不是 NPTL。 On a linuxthreads system, threads have different process IDs.在 linuxthreads 系统上,线程具有不同的进程 ID。

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

At the OS level, theads don't have names;在操作系统层面,主题没有名称; those exist only within the 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. pthread_kill() C 函数可用于向特定线程发送信号,您可以使用该信号尝试从 JVM 外部终止该特定线程,但我不知道 JVM 将如何响应它。 It might just kill the whole JVM.它可能会杀死整个 JVM。

There is no standard;没有标准; this completely depends on the Java implementation which you're using.这完全取决于您使用的 Java 实现。 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.您在 Linux 上看到的是另外一回事:某些版本的 Linux 在进程表中为父进程的每个线程创建一个条目。 These "processes" aren't real processes (in the isolation sense).这些“过程”不是真正的过程(在隔离意义上)。 They are threads which can be listed with the ps command.它们是可以用ps命令列出的线程。 You can find the process which created them by using the parent PID (PPID).您可以使用父 PID (PPID) 找到创建它们的进程。

There is no generic solution how Java threads are mapped to OS threads, if at all. Java 线程如何映射到 OS 线程(如果有的话)没有通用的解决方案。 Every JVM implementation can do it in a different way.每个 JVM 实现都可以用不同的方式来实现。

There is also a pure Java thread implementation, called green threads .还有一个纯 Java 线程实现,称为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 ?一个正在运行的 Java 线程能否被另一个 Java 代码挂起或杀死?

If they are running at the same JVM, yes, with stop().如果它们在同一个 JVM 上运行,是的,使用 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.没有办法杀死我所知道的 JVM 之外的线程。 If a OS really supports killing of threads, I wouldn't expect the Java application to run correctly afterwards!如果操作系统真的支持终止线程,那么我不希望 Java 应用程序之后能正确运行!

Can a running Java threads can be suspended or killed from another Java code ?一个正在运行的 Java 线程能否被另一个 Java 代码挂起或杀死?

In theory yes.理论上是的。 In practice, the Thread.kill() and Thread.suspend() methods are deprecated because they are unsafe except in very limited situations.实际上,不推荐使用Thread.kill()Thread.suspend()方法,因为它们是不安全的,除非在非常有限的情况下。 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.基本问题是,杀死或挂起 Java 线程可能会弄乱依赖它的其他线程,以及它可能正在更新的共享数据结构。

If "another Java code" is meant to mean another JVM, then the chances of it working are even less.如果“另一个 Java 代码”意味着另一个 JVM,那么它工作的机会就更少了。 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.我敢打赌,“目标”JVM 会崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM