Java线程是否具有自己的进程ID? [英] Does a Java Thread have its own process ID?

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

问题描述

我想获取一个线程的进程ID,以查看它占用了多少内存.

I want to get the process ID of a Thread to see how much memory it takes.

推荐答案

这在很大程度上取决于操作系统以及它如何管理线程.从理论上讲,它还取决于JVM如何实现线程,但是所有现代JVM都将它们实现为本地线程.

It depends a lot on the OS and how it manages threads. Theoretically it also depends on how the JVM implements threads, but all modern JVMs implement them as native threads.

在Linux上,每个线程用于获取自己的进程ID,但是大多数工具每个进程只隐藏一个线程(也就是说,除非明确要求它们,否则通常不会看到它们,ps使用-m标志).这是因为Linux内核实际上并没有在线程和任务之间产生太大的差异.

On Linux each thread will used to get its own process ID, but most tools hide all but one thread per process (i.e. you don't usually see them unless you explicitly ask for them, ps uses the -m flag for example). This is caused by the fact that the Linux kernel doesn't really make much of a difference between threads and tasks.

正如我刚刚了解到的,这不再是必要的情况:您可以创建一个与PID完全相同的线程父级,在这种情况下,线程将通过不同的线程ID进行区分.

as I just learned this is no longer necessarily the case: you can create a thread with the exact same PID as the parent, in which case the threads will be distinguished by different thread IDs.

但是,因为一个线程与同一进程中的所有其他线程共享内存,所以不会帮助您找出一个线程需要多少内存",因为一个进程中的所有线程都将使用完全相同的数量(并且它们都使用相同的数量,所以实际使用的内存是shown_memory_use而不是shown_memory_user * number_of_threads).

However since a thread shares its memory with all other threads in the same process, this doesn't help you find out "how much memory a thread takes", since all threads in a process will use the exact same amount (and they all use the same, so the real used memory is shown_memory_use and not shown_memory_user * number_of_threads).

这篇关于Java线程是否具有自己的进程ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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