子线程异常会影响父线程吗

4,331 阅读1分钟

答案是不会的

public static void main(String[] args) {

    log.info("主线程开始");

    final Thread thread = new Thread() {
        @Override
        public void run() {
            if (2 > 1) {
                log.info("主线程");
                throw new RuntimeException("主线程");
            }
        }
    };
    thread.start();
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    log.info("主线程结束");
}