如何在Java中获取线程信息/统计信息
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java中获取线程信息/统计信息,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2468字,纯文字阅读大概需要4分钟。
内容图文
![如何在Java中获取线程信息/统计信息](/upload/InfoBanner/zyjiaocheng/676/963581ddd5784560b41cba53280f2a65.jpg)
标题说明了一切.我下面包含一些代码,我想知道如何去获取与线程相关的统计信息/信息(即正在运行多少个不同的线程,不同的线程的名称).为了一致性起见,使用22 33 44 55作为命令行参数运行该代码.
我也想知道在此特定示例中try块的目的是什么.我了解try块通常会做什么,但是具体来说try块对线程有什么作用.
public class SimpleThreads {
//Display a message, preceded by the name of the current thread
static void threadMessage(String message) {
long threadName = Thread.currentThread().getId();
System.out.format("id is %d: %s%n", threadName, message);
}
private static class MessageLoop implements Runnable {
String info[];
MessageLoop(String x[]) {
info = x;
}
public void run() {
try {
for (int i = 1; i < info.length; i++) {
//Pause for 4 seconds
Thread.sleep(4000);
//Print a message
threadMessage(info[i]);
}
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
}
}
}
public static void main(String args[])throws InterruptedException {
//Delay, in milliseconds before we interrupt MessageLoop
//thread (default one minute).
long extent = 1000 * 60;//one minute
String[] nargs = {"33","ONE", "TWO"};
if (args.length != 0) nargs = args;
else System.out.println("assumed: java SimpleThreads 33 ONE TWO");
try {
extent = Long.parseLong(nargs[0]) * 1000;
} catch (NumberFormatException e) {
System.err.println("First Argument must be an integer.");
System.exit(1);
}
threadMessage("Starting MessageLoop thread");
long startTime = System.currentTimeMillis();
Thread t = new Thread(new MessageLoop(nargs));
t.start();
threadMessage("Waiting for MessageLoop thread to finish");
//loop until MessageLoop thread exits
int seconds = 0;
while (t.isAlive()) {
threadMessage("Seconds: " + seconds++);
//Wait maximum of 1 second for MessageLoop thread to
//finish.
t.join(1000);
if (((System.currentTimeMillis() - startTime) > extent) &&
t.isAlive()) {
threadMessage("Tired of waiting!");
t.interrupt();
//Shouldn't be long now -- wait indefinitely
t.join();
}
}
threadMessage("All done!");
}
}
解决方法:
您可以使用VisualVM进行线程监视.它包含在JDK 6 update 7和更高版本中.您可以在JDK path / bin文件夹中找到visualVm.
VisualVM presents data for local and remote applications in a tab
specific for that application. Application tabs are displayed in the
main window to the right of the Applications window. You can have
multiple application tabs open at one time. Each application tab
contains sub-tabs that display different types of information about
the application.VisualVM displays real-time, high-level data on
thread activity in the Threads tab.
内容总结
以上是互联网集市为您收集整理的如何在Java中获取线程信息/统计信息全部内容,希望文章能够帮你解决如何在Java中获取线程信息/统计信息所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。