《程序员》杂志去年的合订本里有一篇文章,介绍线程比较简单、详细
你可以看看
你运行一个程序,线程中不仅有你的程序,还有系统的线程
取得ThreadGroup这个list吧,然后打印一下里面的信息,很清晰的

解决方案 »

  1.   

    to freejacky(免费的杜松子酒):
    有没有源码呀??
      

  2.   

    public class ThreadGruopDemo
    {    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一下看看
    技术没什么高深的,重要的是你的思想、你的设计
      

  3.   

    http://eaoo.com/design/list.asp?classid=2&Nclassid=14
      

  4.   

    to freejacky(免费的杜松子酒):
    嗯,不错,我待会有空的时候测测看,好了就结贴
      

  5.   

    public static void main(String[] args)
    {
    ThreadGroup top = null;
    ThreadGroup current = Thread.currentThread().getThreadGroup();
    while(current != null)
    {
    top = current;
    current = current.getParent();
    }
    top.list();
    }
      

  6.   

    to cbhyk():
    .list列出来的信息是什么意思?有没有相关文档解释呀???
      

  7.   

    public void list()
        Prints information about this thread group to the standard output. This method is useful only for debugging.
      

  8.   

    to freejacky(免费的杜松子酒):
    可能是我不太会用,感觉帮助还不是很大to cbhyk():
    .list只能是debug用??
    我希望达到的效果是这样的,我能够列出程序运行进程内的所有线程,包括它们的当前运行状态,每个线程都有一个唯一的标识号来标识,通过标识号,我可以找到线程所属的类,并且可以取得这个类对象的句柄,给该线程的属性赋值,调用线程里的方法,从而达来线程管理的目的,而并不是只简单的列出线程的优先级这样的信息,能有其它办法吗??
      

  9.   

    to cbhyk():
    谢谢,可惜我手头上没有api参考手册,我只能通过Thread.来查看Thread类的方法和属性,但这样很难猜得准哪一个是什么意思,呵呵
      

  10.   

    to cbhyk():
    哦,我到http://java.sun.com/j2se/1.4.1/docs/api/找到参考了,在看,希望能够真的解决问题,先谢谢你了
      

  11.   

    我试了用setName方法来设置唯一,但如何根据这个唯一调用里面的方法或设置属性好像就不行了,因为我的类是从Thread类继承而来的,难道线程真的是如此难管理吗??还是需要重新专门设置一个线程管理类来进行管理呢??没有人做过吗??
      

  12.   

    String name = ...//要操作线程的名称
    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进行操作