Java并发编程实例--11.在线程组中处理未检查异常
第8个例子讲了如何在线程中捕捉未检查异常,本例将介绍如何在线程组中处理未检查异常。
- Task.java
package com.dylan.thread.ch1.c11.task;
import java.util.Random;
/**
* Class that implements the concurrent task
*
*/
public class Task implements Runnable {
@Override
public void run() {
int result;
// Create a random number generator
Random random=new Random(Thread.currentThread().getId());
while (true) {
// Generate a random number a calculate 1000 divide by that random number
result=1000/((int)(random.nextDouble()*1000));
System.out.printf("%s : %f\n",Thread.currentThread().getId(),result);
// Check if the Thread has been interrupted
if (Thread.currentThread().isInterrupted()) {
System.out.printf("%d : Interrupted\n",Thread.currentThread().getId());
return;
}
}
}
}
- MyThreadGroup.java
package com.dylan.thread.ch1.c11.group;
/**
* Class that extends the ThreadGroup class to implement
* a uncaught exceptions method
*
*/
public class MyThreadGroup extends ThreadGroup {
/**
* Constructor of the class. Calls the parent class constructor
* @param name
*/
public MyThreadGroup(String name) {
super(name);
}
/**
* Method for process the uncaught exceptions
*/
@Override
public void uncaughtException(Thread t, Throwable e) {
// Prints the name of the Thread
System.out.printf("The thread %s has thrown an Exception\n",t.getId());
// Print the stack trace of the exception
e.printStackTrace(System.out);
// Interrupt the rest of the threads of the thread group
System.out.printf("Terminating the rest of the Threads\n");
interrupt();
}
}
- Main.java
package com.dylan.thread.ch1.c11.core;
import com.dylan.thread.ch1.c11.group.MyThreadGroup;
import com.dylan.thread.ch1.c11.task.Task;
/**
* Main class of the example
*
*/
public class Main {
/**
* Main method of the example. Creates a group of threads of
* MyThreadGroup class and two threads inside this group
* @param args
*/
public static void main(String[] args) {
// Create a MyThreadGroup object
MyThreadGroup threadGroup=new MyThreadGroup("MyThreadGroup");
// Create a Taks object
Task task=new Task();
// Create and start two Thread objects for this Task
for (int i=0; i<2; i++){
Thread t=new Thread(threadGroup,task);
t.start();
}
}
}
运行结果:
10 : 11 : The thread 10 has thrown an Exception
The thread 11 has thrown an Exception
java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at com.dylan.thread.ch1.c11.task.Task.run(Task.java:19)
at java.lang.Thread.run(Thread.java:745)
java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at com.dylan.thread.ch1.c11.task.Task.run(Task.java:19)
at java.lang.Thread.run(Thread.java:745)
Terminating the rest of the Threads
Terminating the rest of the Threads
Java并发编程实例--11.在线程组中处理未检查异常的更多相关文章
- 2、Java并发编程:如何创建线程
Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...
- 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 . 正文 ...
- 原创】Java并发编程系列2:线程概念与基础操作
[原创]Java并发编程系列2:线程概念与基础操作 伟大的理想只有经过忘我的斗争和牺牲才能胜利实现. 本篇为[Dali王的技术博客]Java并发编程系列第二篇,讲讲有关线程的那些事儿.主要内容是如下这 ...
- Java 并发编程——Executor框架和线程池原理
Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...
- Java 并发编程——Executor框架和线程池原理
Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...
- Java并发编程 | 从进程、线程到并发问题实例解决
计划写几篇文章讲述下Java并发编程,帮助一些初学者成体系的理解并发编程并实际使用,而不只是碎片化的了解一些Synchronized.ReentrantLock等技术点.在讲述的过程中,也想融入一些相 ...
- [转载] java并发编程:Lock(线程锁)
作者:海子 原文链接: http://www.cnblogs.com/dolphin0520/p/3923167.html 出处:http://www.cnblogs.com/dolphin0520/ ...
随机推荐
- [转帖]从小白到精通:揭秘perf工具的全部功能与操作技巧
https://zhuanlan.zhihu.com/p/664396453 目录 收起 一.引言 二.理解perf工具的基本概念 三.安装与配置perf工具 3.1.不同操作系统的perf工具安 ...
- 部分MySQL的SQL信息整理
模块补丁信息查看 select su as 补丁模块, count(1) as 数量 from gsppatchlog where TO_DAYS( NOW( ) ) - TO_DAYS(deploy ...
- [转帖] 这grep咋还不支持\d呢(BRE,ERE,PCRE)
https://www.cnblogs.com/codelogs/p/16060372.html 简介# 对于刚使用Linux不久的同学,肯定会遇到这个问题,就是用grep匹配数字时,发现\d匹配不了 ...
- 麒麟信安V3.4 安装PG15的过程V2.0
改版说明 上一次进行了PG的安装当时发现自己少加了一部分ossp的处理. 这里补充上...不然挺浪费时间的. 背景 发现基于OpenEuler的几个系统使用CentOS的rpm包 安装PG数据库时有问 ...
- openEuler technical-certification
https://gitee.com/meitingli/technical-certification/ 介绍 存放openEuler技术测评相关的文档,包括技术测评标准.流程.指导性文档等 技术测评 ...
- 六个节点三主三从Redis集群最简单搭建方法
背景 一个服务器上面的三主三从的界面太low,容易出问题. 为了验证高可用, 我这边使用六台机器进行了三主三从的搭建. 模仿开源版的一键搭建集群的脚本进行使用,感觉非常简单,这里简单进行一下总结. 环 ...
- log4j-漏洞修复
Log4j漏洞修复 修复参考文档:https://www.cert.org.cn/publish/main/9/2021/20211215154225883558274/202112151542258 ...
- bean的一生
你曾读spring源码 "不知所云"."绞尽脑汁"."不知所措"嘛 那这篇文章可能会对你有所帮助,小编尝试用简单.易懂的例子来模拟sprin ...
- 手写promise完成异常处理和状态只能够修改一次
8.处理 Promise 抛出的异常 <script src="./Promise.js"></script> <script type=" ...
- 【K哥爬虫普法】字节前高管,离职后入侵今日头条数据库,是阴谋、还是利诱?
案情介绍 2016年至2017年间,张洪禹.宋某.侯明强作为被告单位上海晟品网络科技有限公司主管人员,在上海市共谋采用技术手段抓取北京字节跳动网络技术有限公司(办公地点位于本市海淀区北三环西路43号中 ...