Java并发编程实例--8.在线程中处理未检查异常
java中有两类异常:
已检查异常:这类异常编译器要求开发者必须在代码中通过throws去处理。
例如:IOException和ClassNotFoundException。
未检查异常:不必显式的在代码重处理。例如:NumberFormatException。
所有派生自Error和RuntimeException的类,都是未检查异常.其余的是已检查异常.
当一个已检查异常在线程对象的run()方法中抛出,我们就必须去捕捉并处理它,因为run()方法不接受throws条件。
而当一个未检查异常在run()方法中抛出,其默认行为是将异常信息输出到控制台并退出程序。
幸运的是,Java提供了在线程中捕捉和处理未检查异常的机制,以避免程序结束。
本例就来学习一下这一机制。
Task.java
package com.dylan.thread.ch1.c08.task;
/**
* Runnable class than throws and Exception
*
*/
public class Task implements Runnable {
/**
* Main method of the class
*/
@Override
public void run() {
// The next instruction always throws and exception
int numero=Integer.parseInt("TTT");
}
}
ExceptionHandler.java
package com.dylan.thread.ch1.c08.handler;
import java.lang.Thread.UncaughtExceptionHandler;
/**
* Class that process the uncaught exceptions throwed in a Thread
*
*/
public class ExceptionHandler implements UncaughtExceptionHandler {
/**
* Main method of the class. It process the uncaught excpetions throwed
* in a Thread
* @param t The Thead than throws the Exception
* @param e The Exception throwed
*/
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.printf("An exception has been captured\n");
System.out.printf("Thread: %s\n",t.getId());
System.out.printf("Exception: %s: %s\n",e.getClass().getName(),e.getMessage());
System.out.printf("Stack Trace: \n");
e.printStackTrace(System.out);
System.out.printf("Thread status: %s\n",t.getState());
}
}
Main.java
package com.dylan.thread.ch1.c08.core;
import com.dylan.thread.ch1.c08.handler.ExceptionHandler;
import com.dylan.thread.ch1.c08.task.Task;
/**
* Main class of the example. Initialize a Thread to process the uncaught
* exceptions and starts a Task object that always throws an exception
*
*/
public class Main {
/**
* Main method of the example. Initialize a Thread to process the
* uncaught exceptions and starts a Task object that always throws an
* exception
* @param args
*/
public static void main(String[] args) {
// Creates the Task
Task task=new Task();
// Creates the Thread
Thread thread=new Thread(task);
// Sets de uncaugh exceptio handler
thread.setUncaughtExceptionHandler(new ExceptionHandler());
// Starts the Thread
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.printf("Thread has finished\n");
}
}
运行结果:
An exception has been captured
Thread: 10
Exception: java.lang.NumberFormatException: For input string: "TTT"
Stack Trace:
java.lang.NumberFormatException: For input string: "TTT"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.dylan.thread.ch1.c08.task.Task.run(Task.java:16)
at java.lang.Thread.run(Thread.java:745)
Thread status: RUNNABLE
Thread has finished
Java并发编程实例--8.在线程中处理未检查异常的更多相关文章
- Java并发编程 | 从进程、线程到并发问题实例解决
计划写几篇文章讲述下Java并发编程,帮助一些初学者成体系的理解并发编程并实际使用,而不只是碎片化的了解一些Synchronized.ReentrantLock等技术点.在讲述的过程中,也想融入一些相 ...
- Java并发编程:如何创建线程?
Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...
- 【转】Java并发编程:如何创建线程?
一.Java中关于应用程序和进程相关的概念 在Java中,一个应用程序对应着一个JVM实例(也有地方称为JVM进程),一般来说名字默认是java.exe或者javaw.exe(windows下可以通过 ...
- [Java并发编程(二)] 线程池 FixedThreadPool、CachedThreadPool、ForkJoinPool?为后台任务选择合适的 Java executors
[Java并发编程(二)] 线程池 FixedThreadPool.CachedThreadPool.ForkJoinPool?为后台任务选择合适的 Java executors ... 摘要 Jav ...
- [Java并发编程(一)] 线程池 FixedThreadPool vs CachedThreadPool ...
[Java并发编程(一)] 线程池 FixedThreadPool vs CachedThreadPool ... 摘要 介绍 Java 并发包里的几个主要 ExecutorService . 正文 ...
- 2、Java并发编程:如何创建线程
Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...
- 原创】Java并发编程系列2:线程概念与基础操作
[原创]Java并发编程系列2:线程概念与基础操作 伟大的理想只有经过忘我的斗争和牺牲才能胜利实现. 本篇为[Dali王的技术博客]Java并发编程系列第二篇,讲讲有关线程的那些事儿.主要内容是如下这 ...
- Java 并发编程——Executor框架和线程池原理
Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...
- Java 并发编程——Executor框架和线程池原理
Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...
- [转载] java并发编程:Lock(线程锁)
作者:海子 原文链接: http://www.cnblogs.com/dolphin0520/p/3923167.html 出处:http://www.cnblogs.com/dolphin0520/ ...
随机推荐
- [转帖]Oracle 12.2 新特性 | PDB不同字符集变更
https://www.cnblogs.com/cqdba/p/8bef7c432b87807c0680d6791f427b09.html 在oracle12.1版本中,同一CDB中的所有PDB使用的 ...
- [转帖]前端安全(同源策略、XSS攻击、CSRF攻击)
https://juejin.cn/post/6844904158697357319 同源策略(Same-origin policy) 如果两个 URL 的协议.域名和端口都相同,我们就称这两个 UR ...
- [转帖]在麒麟Linux安装Postgis
https://jimolonely.github.io/tech/linux/install-postgis-kylin/ 接着上一篇在麒麟linux上安装Postgresql12.5 ,我们来安装 ...
- 物理机和虚拟机上CPU睿频的区别
物理机和虚拟机上CPU睿频的区别 关于睿频 睿频是指当启动一个运行程序后,处理器会自动加速到合适的频率, 而原来的运行速度会提升 10%~20% 以保证程序流畅运行的一种技术. 一般max的睿频不能超 ...
- [转帖]将nginx.conf文件的内容拆分成多个
nginx的如果有多个server模块都配置在同一个nginx.conf文件会显得比较臃肿,后续维护起来也会比较困难,所以可以将内容写入到多个配置文件中然后在nginx.conf文件中通过includ ...
- [转帖]线上Java 高CPU占用、高内存占用排查思路
一.前言 处理过线上问题的同学基本上都会遇到系统突然运行缓慢,CPU 100%,以及Full GC次数过多的问题.当然,这些问题的最终导致的直观现象就是系统运行缓慢,并且有大量的报警.本文主要针对系统 ...
- 记录TritonServer部署多模型到多GPU踩坑 | 京东云技术团队
一.问题是怎么发现的 部署chatglm2和llama2到一个4*V100的GPU机器上遇到问题 config.pbtxt 中设置模型分别在指定gpu上部署实例配置不生效 如以下配置为在gpu0上部署 ...
- 巧用GenericObjectPool创建自定义对象池
作者:京东物流 高圆庆 1 前言 通常一个对象创建.销毁非常耗时的时候,我们不会频繁的创建和销毁它,而是考虑复用.复用对象的一种做法就是对象池,将创建好的对象放入池中维护起来,下次再用的时候直接拿池中 ...
- 用户 'NT Service\SSISScaleOutMaster140' 登录失败
用户 'NT Service\SSISScaleOutMaster140' 登录失败. 原因: 找不到与提供的名称匹配的登录名. 项目情况: 用户 'NT Service\SSISScaleOutMa ...
- [3] 以逆向的角度来看循环语句——do、while、for的比较
[3] 以逆向的角度来看循环语句--do.while.for的比较 1. do循环 先执行循环体,后比较判断 #include <stdio.h> int main(int argc, ...