• 《Java多线程编程核心技术》读后感(十六)


     

     

    线程组

    线程组的作用是,可以批量的管理线程或线程组对象,有效地对线程或线程组对象进行组织

    线程对象关联线程组:1级关联

    package Seven;
    
    public class ThreadA  extends Thread {
    
        @Override
        public void run() {
            try {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println("ThreadName=" + Thread.currentThread().getName());
                    Thread.sleep(3000);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    package Seven;
    
    public class ThreadB extends Thread {
    
        @Override
        public void run() {
            try {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println("ThreadName=" + Thread.currentThread().getName());
                    Thread.sleep(3000);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    package Seven;
    
    public class Run {
        public static void main(String[] args) {
            ThreadA aRunnable = new ThreadA();
            ThreadB bRunnable = new ThreadB();
    
            ThreadGroup group = new ThreadGroup("高洪岩的线程组");
    
            Thread aThread = new Thread(group, aRunnable);
            Thread bThread = new Thread(group, bRunnable);
            aThread.start();
            bThread.start();
    
            System.out.println("活动的线程数为:" + group.activeCount());
            System.out.println("线程组的名称为:" + group.getName());
    
        }
    }

     线程对象关联线程组:多级关联

    package Seven;
    
    public class Run {
        public static void main(String[] args) {
    
            // 在main组中添加一个线程组A,然后在这个A组中添加线程对象Z
            // 方法activeGroupCount()和activeCount()的值不是固定的
            // 是系统中环境的一个快照
            ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
            ThreadGroup group = new ThreadGroup(mainGroup, "A");
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("runMethod!");
                        Thread.sleep(10000);// 线程必须在运行状态才可以受组管理
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
    
            Thread newThread = new Thread(group, runnable);
            newThread.setName("Z");
            newThread.start();// 线程必须启动然后才归到组A中
            // ///
            ThreadGroup[] listGroup = new ThreadGroup[Thread.currentThread()
                    .getThreadGroup().activeGroupCount()];
            Thread.currentThread().getThreadGroup().enumerate(listGroup);
            System.out.println("main线程中有多少个子线程组:" + listGroup.length + " 名字为:"
                    + listGroup[0].getName());
            Thread[] listThread = new Thread[listGroup[0].activeCount()];
            listGroup[0].enumerate(listThread);
            System.out.println(listThread[0].getName());
    
        }
    }

    线程组自动归属特性

    自动归属就是自动归到当前线程组中

    package Seven;
    
    public class Run {
        public static void main(String[] args) {
            System.out.println("A处线程:"+Thread.currentThread().getName()
                    + " 所属的线程组名为:"
                    + Thread.currentThread().getThreadGroup().getName()+ " "
                    + " 中有线程组数量:"+Thread.currentThread().getThreadGroup().activeGroupCount());
            ThreadGroup group=new ThreadGroup("新的组");
            System.out.println("B处线程:"+Thread.currentThread().getName()
                    + " 所属的线程组名为:"
                    + Thread.currentThread().getThreadGroup().getName()+ " "
                    +" 中有线程组数量:"+Thread.currentThread().getThreadGroup().activeGroupCount());
            ThreadGroup[] threadGroup=new ThreadGroup[Thread.currentThread().getThreadGroup().activeGroupCount()];
            Thread.currentThread().getThreadGroup().enumerate(threadGroup);
            for (int i = 0; i < threadGroup.length; i++) {
                System.out.println("第一个线程组名称为:"+threadGroup[i].getName());
            }
        }
    }

    未完,待续。。。

  • 相关阅读:
    【问题】解决python3不支持mysqldb
    《Craking the Coding interview》python实现---02
    《Craking the Coding interview》python实现---01
    python标准库--functools.partial
    Multimodal Machine LearningA Survey and Taxonomy
    概率热图的绘制--gradcam
    Pytorch 技巧总结(持续更新)
    linux教训
    Destruction and Construction Learning for Fine-grained Image Recognition----论文
    Ubuntu16.04+3090+cuda11.0+cudnnV8+pytorch-nightly
  • 原文地址:https://www.cnblogs.com/Michael2397/p/7860234.html
Copyright © 2020-2023  润新知