public static void main(String[] args) {
ThreadGroup threadGroup= Thread.currentThread().getThreadGroup().getParent();
//不知道java为什么定义了一个ThreadGroup
Thread[] list=new Thread [threadGroup.activeCount()];
threadGroup.enumerate(list,true);
//enumerate这个做什么用的?
for (Thread thread : list) {
System.out.println(thread.getName()+"优先级:"+thread.getPriority());
}
}结果;
--------------------Configuration: <Default>--------------------
Reference Handler优先级:10
Finalizer优先级:8
Signal Dispatcher优先级:9
Attach Listener优先级:5
main优先级:5为什么Reference Handler的优先级比主线程还高。
ThreadGroup threadGroup= Thread.currentThread().getThreadGroup().getParent();
//不知道java为什么定义了一个ThreadGroup
Thread[] list=new Thread [threadGroup.activeCount()];
threadGroup.enumerate(list,true);
//enumerate这个做什么用的?
for (Thread thread : list) {
System.out.println(thread.getName()+"优先级:"+thread.getPriority());
}
}结果;
--------------------Configuration: <Default>--------------------
Reference Handler优先级:10
Finalizer优先级:8
Signal Dispatcher优先级:9
Attach Listener优先级:5
main优先级:5为什么Reference Handler的优先级比主线程还高。
Copies into the specified array every active thread in this thread group.
sleep之后就会随机排序了,
yep, forget ThreadGroup which is a unsuccessful try made by Sun
Finalizer优先级:8
Signal Dispatcher优先级:9
Attach Listener优先级:5 这几个是JVM的线程啊
猜想高优先级的那两个一个是控制线程通信的,一个用来标记变量引用的(用处之一是用来作垃圾回收(Finalizer?)的)这些东西是保证线程运行良好的东东,你觉得它们不应该高优先级一点吗?
下面代码用于获取几乎所有 JVM 中的活动线程:
public class AllThread { public static Thread[] getAllThread() {
ThreadGroup root = Thread.currentThread().getThreadGroup();
ThreadGroup ttg = root;
while ((ttg = ttg.getParent()) != null) root = ttg;
Thread[] tlist = new Thread[(int)(root.activeCount() * 1.2)];
return java.util.Arrays.copyOf(tlist, root.enumerate(tlist, true));
} public static void main(String[] args) {
Thread[] ts = getAllThread();
for (Thread t : ts) {
System.out.println(t.getId() + ": " + t.getName());
}
}}输出:
2: Reference Handler
3: Finalizer
4: Signal Dispatcher
5: Attach Listener
1: main这几个线程是你启动的?
你那代码能保证获取到所有线程的引用?
搞笑!
前 4 个为 JVM 系统级线程,只要记得别对它们进行操作就行了,至于优先级的高低是根据系统线程的功能和需要来定的。具体功能拿它们名字去 Google。
sleep之后就会随机排序了,
4楼的这番话有没有经过思考的。
只是想知道你为什么这么想?
优先级高的,本来就不一定优先处理!
线程的优先级,在sleep之后,就是会随机排序的!首先,这些我都没有思考,还有,我也没有想过这些东西,官方说的内容,根本不用想!
所以,我不知道你在问什么.
人家官方规范有说明,另外scjp的考试资料也有这些内容!