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/ ...
随机推荐
- [转帖]AF_UNIX和AF_INET
https://www.cnblogs.com/shangerzhong/p/9153737.html family参数代表地址家族,比较常用的为AF_INET或AF_UNIX.AF_UNIX用于同一 ...
- [转帖]perf学习-linux自带性能分析工具
目前在做性能分析的事情,之前没怎么接触perf,找了几篇文章梳理了一下,按照问题的形式记录在这里. 方便自己查看. 什么是perf? linux性能调优工具,32内核以上自带的工具,软件性能分析. ...
- [转帖]s3对象存储挂载到本地文件夹
https://www.zhangzhuo.ltd/articles/2021/10/22/1634888049032.html 一.s3fs工具 s3fs-fuse 是一个采用 c++ 开发的开源应 ...
- [转帖]MioIO读/写性能测试
https://www.jianshu.com/p/a0a84f91b16f image.png COSBench是Intel团队基于java开发,是一个测试云对象存储系统的分布式基准测试工具,全 ...
- 关于cockpit的学习
关于cockpit的学习 背景 使用node-exporter 可以监控很多资源使用情况 但是这个需要搭建一套prometheus和grafana的工具 并且每个机器都需要安装一套node-expor ...
- [转帖]ls命令
ls(list) 命令可以说是Linux下最常用的命令之一 #ls -l;列出文件的详细信息 #ll 以上两个命令一样,ll是ls -l的简写 #ls -al;列出目录下的所有文件,包括以 . 开头的 ...
- jcmd的简单总结
jcmd的简单总结 背景 自从2019年公司转向java技术路线. 一直断断续续的在学习java相关的技术内容. 但是总感觉学的不是很深入. 这周比较累.也不想在学新东西了. 所以想着再总结一下jcm ...
- Linux与Windows系统字符集的简要学习
背景 最近同事反馈公司的产品再更新了mysql-8.0.31的驱动jar包后部分功能报错. 问题核心原因 研发这边石磊老师已经找到了. 结论是Mysql8.0.26之后的数据库驱动好像会识别操作系统的 ...
- unzip 解压缩存在Bug-- 这个方法不行啊
linux中解压大于4G的zip压缩包(已解决) tar -zxvf 压缩包名.zip
- elementUI自定义单选框内容
<template> <div> <div class="heng-div"> <el-radio v-model="radio ...