美文网首页
Java 线程组

Java 线程组

作者: 黑白_a9aa | 来源:发表于2019-05-08 21:41 被阅读0次

    java.lang.ThreadGroup

    1. Java进程中的每一条线程都属于某一线程组
    2. 如果线程实例化的时候没有指定线程组,则默认所属于父线程的线程组
    3. main线程属于【java.lang.ThreadGroup[name=main,maxpri=10]】
    4. 可以有两个同名的线程组,但实际上是两个不同的组

    线程组可实现的一些功能

    通过线程组中断一批线程

    ThreadGroup中的interrupt()方法示例代码

    package test;
    
    import java.util.logging.Logger;
    
    public class Test {
        private static final Logger log = Logger.getLogger(Test.class.getName());
        
        public static void main(String[] args) {
            log.info(Thread.currentThread().getName() + " start.");
            Test t = new Test();
            
            new Thread(new ThreadGroup("group1"), t.new SubManager(), "SubManager1").start();
            new Thread(new ThreadGroup("group2"), t.new SubManager(), "SubManager2").start();
            new Thread(t.new SubManager(), "SubManager3").start();
            
            try {
                Thread.sleep(2250);
            } catch (InterruptedException e) {
                log.severe(e.toString() + " @ " + Thread.currentThread().getName());
            }
            
            Thread.currentThread().getThreadGroup().list();
            Thread.currentThread().getThreadGroup().interrupt();
        }
        
        class SubManager implements Runnable{
    
            @Override
            public void run() {
                while(!Thread.currentThread().isInterrupted()) {
                    log.info(Thread.currentThread().getName() + " is running.");
                    
                    new Thread(new SubRun(), "SubRun").start();
                    
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        log.severe(e.toString() + " @ " + Thread.currentThread().getName());
                        break;
                    }
                }
            }
            
        }
        
        class SubRun implements Runnable{
    
            @Override
            public void run() {
                while(!Thread.currentThread().isInterrupted()) {
                    log.info(Thread.currentThread().getName() + " is running.");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        log.severe(e.toString() + " @ " + Thread.currentThread().getName());
                        break;
                    }
                }
            }
            
        }
        
    }
    

    运行结果

    [Wed May 08 21:24:49 CST 2019] INFO: main start.
    [Wed May 08 21:24:49 CST 2019] INFO: SubManager1 is running.
    [Wed May 08 21:24:49 CST 2019] INFO: SubManager3 is running.
    [Wed May 08 21:24:49 CST 2019] INFO: SubManager2 is running.
    [Wed May 08 21:24:49 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:49 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:49 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:49 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:49 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:49 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubManager1 is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubManager3 is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubManager2 is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:50 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubManager3 is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubManager1 is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubManager2 is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    [Wed May 08 21:24:51 CST 2019] INFO: SubRun is running.
    java.lang.ThreadGroup[name=main,maxpri=10]
        Thread[main,5,main]
        Thread[SubManager3,5,main]
        Thread[SubRun,5,main]
        Thread[SubRun,5,main]
        Thread[SubRun,5,main]
        java.lang.ThreadGroup[name=group1,maxpri=10]
            Thread[SubManager1,5,group1]
            Thread[SubRun,5,group1]
            Thread[SubRun,5,group1]
            Thread[SubRun,5,group1]
        java.lang.ThreadGroup[name=group2,maxpri=10]
            Thread[SubManager2,5,group2]
            Thread[SubRun,5,group2]
            Thread[SubRun,5,group2]
            Thread[SubRun,5,group2]
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubManager2
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubManager1
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubRun
    [Wed May 08 21:24:51 CST 2019] SEVERE: java.lang.InterruptedException: sleep interrupted @ SubManager3
    

    相关文章

      网友评论

          本文标题:Java 线程组

          本文链接:https://www.haomeiwen.com/subject/ltrboqtx.html