zoukankan      html  css  js  c++  java
  • Thread和ThreadGroup

    Thread和ThreadGroup

    学习了:https://www.cnblogs.com/yiwangzhibujian/p/6212104.html  这个里面有Thread的基本内容;

    http://blog.csdn.net/a352193394/article/details/39323427 这个讲解了ThreadGroup

    https://www.cnblogs.com/yy2011/archive/2011/05/05/2037564.html 这个比较了ThreadGroup与Executor

    Thread比如属于一个ThreadGroup,

    源码:

            Thread parent = currentThread();
            SecurityManager security = System.getSecurityManager();
            if (g == null) { // 这个就是ThreadGroup
                /* Determine if it's an applet or not */
    
                /* If there is a security manager, ask the security manager
                   what to do. */
                if (security != null) {
                    g = security.getThreadGroup();
                }
    
                /* If the security doesn't have a strong opinion of the matter
                   use the parent thread group. */
                if (g == null) {
                    g = parent.getThreadGroup();
                }
            }

    Thread.setUncaughtExceptionHandler的意义在于不能修改原线程的情况下,为其增加一个错误处理器。

    package com.stono.thread2;
    
    import java.lang.Thread.UncaughtExceptionHandler;
    
    // UnCaughtException意义在于不能对原线程修改的情况下,为其增加一个错误处理器。
    public class ThreadUncaughtException {
        public static void main(String[] args) {
            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    int a = 1/0;
                }
            });
            t1.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    System.out.println("线程:"+t.getName()+"出现异常,异常信息:"+e);
                    e.printStackTrace();
                }
            });
            t1.start();
        }
    }

    ThreadGroup.enumerate(list)方法把ThreadGroup中的线程拷贝到新的List中;

     在ThreadGroup讲解过程中,一个runnable对象构建了多个线程,这个需要注意一下,TODO

  • 相关阅读:
    httpclient的maven依赖
    阿里云maven仓库镜像
    log4j2在webapp项目中的配置
    web.xml中的filter标签
    mybatis在xml文件中处理大于号小于号的方法
    javaweb(三十八)——mysql事务和锁InnoDB(扩展)
    javaweb(三十八)——事务
    javaweb(三十七)——获得MySQL数据库自动生成的主键
    javaweb学习总结(三十六)——使用JDBC进行批处理
    JavaWeb(三十五)——使用JDBC处理Oracle大数据
  • 原文地址:https://www.cnblogs.com/stono/p/8370494.html
Copyright © 2011-2022 走看看