《程序员》杂志去年的合订本里有一篇文章,介绍线程比较简单、详细
你可以看看
你运行一个程序,线程中不仅有你的程序,还有系统的线程
取得ThreadGroup这个list吧,然后打印一下里面的信息,很清晰的
你可以看看
你运行一个程序,线程中不仅有你的程序,还有系统的线程
取得ThreadGroup这个list吧,然后打印一下里面的信息,很清晰的
解决方案 »
- 2/15 PLS-00201: 必须声明标识符 'DATASET.REFCURSOR'这是什么原因呢
- 菜鸟 想找一个简单易懂的 java 温度计 程序 借鉴学习
- java连接两个不同的数据库系统高手指教
- 一道关于类变量初始化的scjp题,不明白结果为什么是3,而且还可以定义两个同名的变量
- 类与内部类的关系???????在线等。。。。
- 组件获得焦点,并能够作出响应
- 输入int 数据时为什么要调用getString()
- 请那怎么样把1000000转成1,000,000呢。有效的一定给分。
- 在JB6的Application中,为什么AWT组中只找到复选框(checkbox)而找不到单选框(radiobutton)?
- Applet 为什么不能在IE中显示,加分!!!!
- 实时要求很高的文本读写(分不够再加,帮忙顶一下)
- BufferedWriter的缓冲限制
有没有源码呀??
{ public ThreadGruopDemo()
{
} public static void main(String[] args)
{
//列举所有线程和子组
ThreadGroup system = null;
ThreadGroup tg = Thread.currentThread().getThreadGroup(); while (tg != null)
{
system = tg;
tg = tg.getParent();
} if (system != null)
{
Thread[] thds = new Thread[system.activeCount()];
int nthds = system.enumerate(thds);
for (int i = 0; i < nthds; i++)
{
System.out.println(thds[i] + " " + thds[i].isDaemon());
}
}
/*
MyThread mt = new MyThread();
mt.setName("A");
mt.start(); mt = new MyThread();
mt.setName("B");
mt.start(); try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
} Thread.currentThread().getThreadGroup().interrupt();
*/
/*
ThreadGroup tg = new ThreadGroup("subGruop 1");
System.out.println("tg maximum priority = " + tg.getMaxPriority()); Thread t1 = new Thread(tg, "thread 1");
System.out.println("t1 priority = " + t1.getPriority());
t1.setPriority(Thread.NORM_PRIORITY + 1);
System.out.println("t1 priority after setPriority() = " + t1.getPriority()); tg.setMaxPriority(Thread.NORM_PRIORITY - 1);
System.out.println("tg maximum priority after setMaxPriority() = " + tg.getMaxPriority());
System.out.println("t1 priority after setMaxPriority() = " + t1.getPriority()); Thread t2 = new Thread(tg, "thread 2");
System.out.println("t2 priority = " + t2.getPriority());
t2.setPriority(Thread.NORM_PRIORITY);
System.out.println("t2 priority after setPriority() = " + t2.getPriority()); Thread t3 = new Thread(tg, "thread 3"); tg = new ThreadGroup("subGruop 2");
Thread t4 = new Thread(tg, "thread 4"); tg = Thread.currentThread().getThreadGroup();
int agc = tg.activeGroupCount();
System.out.println("Active thread gruops in " + tg.getName() + " thread gruop: " + agc);
tg.list();
*/
}
}看对你有没有帮助吧,注释里的内容是其他功能调用,你可以分别取消注释
然后debug一下看看
技术没什么高深的,重要的是你的思想、你的设计
嗯,不错,我待会有空的时候测测看,好了就结贴
{
ThreadGroup top = null;
ThreadGroup current = Thread.currentThread().getThreadGroup();
while(current != null)
{
top = current;
current = current.getParent();
}
top.list();
}
.list列出来的信息是什么意思?有没有相关文档解释呀???
Prints information about this thread group to the standard output. This method is useful only for debugging.
可能是我不太会用,感觉帮助还不是很大to cbhyk():
.list只能是debug用??
我希望达到的效果是这样的,我能够列出程序运行进程内的所有线程,包括它们的当前运行状态,每个线程都有一个唯一的标识号来标识,通过标识号,我可以找到线程所属的类,并且可以取得这个类对象的句柄,给该线程的属性赋值,调用线程里的方法,从而达来线程管理的目的,而并不是只简单的列出线程的优先级这样的信息,能有其它办法吗??
谢谢,可惜我手头上没有api参考手册,我只能通过Thread.来查看Thread类的方法和属性,但这样很难猜得准哪一个是什么意思,呵呵
哦,我到http://java.sun.com/j2se/1.4.1/docs/api/找到参考了,在看,希望能够真的解决问题,先谢谢你了
Thread[] threads
...//通过ThreadGroup的enumerate方法取到Thread数组
Thread theThread;
for(int i=0; i<threads.length; i++)
{
if(threads[i].getName().equals(name);
{
theThread = threads[i];
break;
}
}
...//对线程theThread进行操作