package com.threads;
import java.util.Arrays;
import java.util.Map;
public class TestThread extends Thread {
public void run() {
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
for(Thread s: map.keySet()){
System.out.println(s + Arrays.toString(map.get(s)));
}
}
public static void main(String[] args) {
TestThread testThread = new TestThread();
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
for(Thread s: map.keySet()){
System.out.println(s + Arrays.toString(map.get(s)));
}
System.out.println("-----------------------------------------");
testThread.start();
}
}
Thread[main,5,main][java.lang.Thread.dumpThreads(Native Method), java.lang.Thread.getAllStackTraces(Unknown Source), com.threads.TestThread.main(TestThread.java:17)]
Thread[Finalizer,8,system][java.lang.Object.wait(Native Method), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)]
Thread[Reference Handler,10,system][java.lang.Object.wait(Native Method), java.lang.Object.wait(Unknown Source), java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)]
Thread[Attach Listener,5,system][]
Thread[Signal Dispatcher,9,system][]
---------------------------------
Thread[Thread-0,5,main][java.lang.Thread.dumpThreads(Native Method), java.lang.Thread.getAllStackTraces(Unknown Source), com.threads.TestThread.run(TestThread.java:8)]
Thread[Finalizer,8,system][java.lang.Object.wait(Native Method), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)]
Thread[Reference Handler,10,system][java.lang.Object.wait(Native Method), java.lang.Object.wait(Unknown Source), java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)]
Thread[Attach Listener,5,system][]
Thread[Signal Dispatcher,9,system][]
自从我开始一个新线程以来,我希望在我正在打印的Map中找到另一个条目。但是线程的数量保持不变。
答案 0 :(得分:3)
数字保持不变,但是出现了一个新线程:Thread-0,另一个已经消失:main。
这是预料之中的,因为一旦主方法返回主线程就会退出,而Thread-0就是你已经开始的线程。
答案 1 :(得分:1)
线程数相同,因为您的主线程已在TestThread
正在访问Thread.getAllStackTraces()
的位置完成了该作业。
在主要帖子中,您可以join()并等待TestThread
完成。
public static void main(String[] args) throws InterruptedException {
TestThread testThread = new TestThread();
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
for(Thread s: map.keySet()){
System.out.println(s + Arrays.toString(map.get(s)));
}
System.out.println("-----------------------------------------");
testThread.start();
testThread.join();
}
产生以下结果:
Thread[main,5,main][java.lang.Thread.dumpThreads(Native Method), java.lang.Thread.getAllStackTraces(Unknown Source), code.TestThread.main(TestThread.java:17)]
Thread[Attach Listener,5,system][]
Thread[Signal Dispatcher,9,system][]
Thread[Reference Handler,10,system][java.lang.Object.wait(Native Method), java.lang.Object.wait(Object.java:503), java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)]
Thread[Finalizer,8,system][java.lang.Object.wait(Native Method), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)]
-----------------------------------------
Thread[main,5,main][java.lang.Object.wait(Native Method), java.lang.Thread.join(Unknown Source), java.lang.Thread.join(Unknown Source), code.TestThread.main(TestThread.java:23)]
Thread[Attach Listener,5,system][]
Thread[Signal Dispatcher,9,system][]
Thread[Thread-0,5,main][java.lang.Thread.dumpThreads(Native Method), java.lang.Thread.getAllStackTraces(Unknown Source), code.TestThread.run(TestThread.java:8)]
Thread[Reference Handler,10,system][java.lang.Object.wait(Native Method), java.lang.Object.wait(Object.java:503), java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)]
Thread[Finalizer,8,system][java.lang.Object.wait(Native Method), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.ReferenceQueue.remove(Unknown Source), java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)]