search
HomeJavajavaTutorialJava implementation of multi-threaded exception capture Runnable case

This article mainly introduces relevant information on the implementation of multi-threaded exception capture Runnable in Java. I hope this article can help everyone understand and master such knowledge. Friends in need can refer to it

Detailed explanation of the implementation of multi-threaded exception capture Runnable in Java

1. Background:

Java multi-threaded exceptions are not thrown to the main thread, handled by themselves, external The exception cannot be caught. Therefore, it is necessary to realize the main thread's capture of sub-thread exceptions.
2, tool:

Layerinittask class, ThreadException class, Vector
3, Think Enter the Vector, record exceptions, traverse externally, judge, and throw exceptions.

4. Code: ​


package step5.exception; 
 
import java.util.Vector; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.TimeUnit; 
 
import com.autonavi.pds.core.incre.impl.LayerInitTask; 
 
public class ThreadException { 
 
  public static void main(String[] args) { 
    try { 
      Vector<String> errRet = new Vector(); 
      ExecutorService pool = Executors.newFixedThreadPool(6); 
      for (int i = 0; i < 6; ++i) { 
        pool.execute(new LayerInitTask(i, errRet)); 
      } 
      pool.shutdown(); 
      pool.awaitTermination(1, TimeUnit.DAYS); 
       
      if (errRet.size() > 0) { 
        System.out.println("根据返回值捕获:exception"); 
        throw new RuntimeException( "入库失败!"); 
      } 
       
    } catch (Exception e) { 
      System.out.println("根据抛出异常捕获:exception"); 
      throw new RuntimeException( "入库失败!"); 
    } 
    System.out.println("-----入库成功,发成功完成工作邮件--------"); 
  } 
 
}


package step5.exception; 
 
import java.util.Vector; 
 
public class LayerInitTask implements Runnable { 
  private int threadNum; 
  private Vector<String> errRet; 
 
  public LayerInitTask(int num, Vector<String> errRet) { 
    this.threadNum = num; 
    this.errRet = errRet; 
  } 
 
  @Override 
  public void run() { 
    try { 
      if (this.threadNum == 3) { 
        throw new RuntimeException( this.threadNum + ":数据格式有误."); 
      } 
      System.out.println(this.threadNum + ":刷表成功"); 
    } catch (Exception e) { 
      this.errRet.add("线程:" + this.threadNum + "运行异常!"); 
      throw new RuntimeException( this.threadNum + ":刷表失败"); 
    } 
  } 
 
}

5. Result:


Exception in thread "pool-1-thread-4" java.lang.RuntimeException: 3:刷表失败 
  at step5.exception.LayerInitTask.run(LayerInitTask.java:23) 
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
  at java.lang.Thread.run(Unknown Source) 
Exception in thread "main" java.lang.RuntimeException: 入库失败! 
  at step5.exception.ThreadException.main(ThreadException.java:27) 
2:刷表成功 
1:刷表成功 
5:刷表成功 
0:刷表成功 
4:刷表成功 
根据返回值捕获:exception 
根据抛出异常捕获:exception

The above is the detailed content of Java implementation of multi-threaded exception capture Runnable case. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.