Why can't I catch exceptions in java multithreading ?, Java Multithreading

Source: Internet
Author: User

Why can't I catch exceptions in java multithreading ?, Java Multithreading

Raise questions:

Many Java beginners may see the following code when learning java multithreading:

public class ExceptionThread  implements  Runnable{    @Override    public void run() {        throw new RuntimeException();    }    public static void main(String[] args) {        try{            ExecutorService exec= Executors.newCachedThreadPool();            exec.execute(new ExceptionThread());        }catch(RuntimeException e){            System.out.println("Exception has been handled");        }    }}

The running result is as follows:

This code has already caught an Exception in try-catch. Why didn't we execute System. out. println ("Exception has been handled ")?

Answer:

For example.

After school, the children lined up and followed the teacher to the school gate. Teachers stop, students stop; teachers and students stop. It cannot be a blind run.

At the door, there were 20 parents and 20 children who went back to their families. The teacher also went home.

On-campus routes, teachers and students can be viewed as in a thread, executed in sequence, paused at the front, and required at the back. When the door is reached, the parent receives a thread, which is equivalent to a thread with 20 parents and 20 threads. Each thread is responsible for sending its children home, and there is no sequential dependency between threads, at the same time.

If an exception occurs in a thread, for example, when a child falls on the way home with his parents, he or she will go to the hospital. Obviously, this does not affect the children's home, nor should they be put in the care of their teachers. It is handled by the parent of the injured child. That is to say, the exception should be handled by the thread where it is located. Other threads have no obligation or context to handle your exception.

Essentially, dividing a thread means you don't have to wait. Just like a teacher does not have to wait until all the children arrive home to get off work. Don't wait, it means you don't know the progress of other threads, and you don't have to deal with other thread problems.

When the child falls down, the teacher may already be home. It is like an exception in the new thread in the code of the subject, and the original thread may have run out.

I have a public account that will often share some Java-related things. If you like it, you can search for "Java headers" or "javatuanzhang.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.