第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.在线程组中处理未检查异常的更多相关文章

  1. 2、Java并发编程:如何创建线程

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  2. Java并发编程:如何创建线程?

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  3. 【转】Java并发编程:如何创建线程?

    一.Java中关于应用程序和进程相关的概念 在Java中,一个应用程序对应着一个JVM实例(也有地方称为JVM进程),一般来说名字默认是java.exe或者javaw.exe(windows下可以通过 ...

  4. [Java并发编程(二)] 线程池 FixedThreadPool、CachedThreadPool、ForkJoinPool?为后台任务选择合适的 Java executors

    [Java并发编程(二)] 线程池 FixedThreadPool.CachedThreadPool.ForkJoinPool?为后台任务选择合适的 Java executors ... 摘要 Jav ...

  5. [Java并发编程(一)] 线程池 FixedThreadPool vs CachedThreadPool ...

    [Java并发编程(一)] 线程池 FixedThreadPool vs CachedThreadPool ... 摘要 介绍 Java 并发包里的几个主要 ExecutorService . 正文 ...

  6. 原创】Java并发编程系列2:线程概念与基础操作

    [原创]Java并发编程系列2:线程概念与基础操作 伟大的理想只有经过忘我的斗争和牺牲才能胜利实现. 本篇为[Dali王的技术博客]Java并发编程系列第二篇,讲讲有关线程的那些事儿.主要内容是如下这 ...

  7. Java 并发编程——Executor框架和线程池原理

    Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...

  8. Java 并发编程——Executor框架和线程池原理

    Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...

  9. Java并发编程 | 从进程、线程到并发问题实例解决

    计划写几篇文章讲述下Java并发编程,帮助一些初学者成体系的理解并发编程并实际使用,而不只是碎片化的了解一些Synchronized.ReentrantLock等技术点.在讲述的过程中,也想融入一些相 ...

  10. [转载] java并发编程:Lock(线程锁)

    作者:海子 原文链接: http://www.cnblogs.com/dolphin0520/p/3923167.html 出处:http://www.cnblogs.com/dolphin0520/ ...

随机推荐

  1. k8s~istio的安装与核心组件

    安装istio 在线安装:https://istio.io/latest/docs/setup/getting-started/#download 或者直接在这里下载:https://github.c ...

  2. [转帖]SQL Server 性能调优

      性能调优2:CPU   关系型数据库严重依赖底层的硬件资源,CPU是服务器的大脑,当CPU开销很高时,内存和硬盘系统都会产生不必需要的压力.CPU的性能问题,直观来看,就是任务管理器中看到的CPU ...

  3. [转帖]TiDB 内存控制文档

    https://docs.pingcap.com/zh/tidb/stable/configure-memory-usage 目前 TiDB 已经能够做到追踪单条 SQL 查询过程中的内存使用情况,当 ...

  4. [转帖]clickhouse存储机制以及底层数据目录分布

    https://www.cnblogs.com/MrYang-11-GetKnow/p/15818141.html#:~:text=%E6%AF%8F%E4%B8%80%E4%B8%AA%E6%95% ...

  5. Linux用户以及ssh安全相关设置

    Linux用户相关操作 摘要 最近重保, 需要进行网络安全防护. 部分同事处理过程总是顺序有一些不太对的情况. 同时发现自对Linux用户设置也存在很多不清不楚的地方 所以趁着周末学习和总结一下. 用 ...

  6. 【构造,树】【Loj】Loj6669 Nauuo and Binary Tree

    2023.7.3 Problem Link 交互库有一棵 \(n\) 个点的二叉树,你每次可以询问两个点之间的距离,猜出这棵二叉树.\(n\le 3000\),询问次数上限 \(30000\). 首先 ...

  7. uni-app 计算属性 computed

    功能:=>大于1000用kg表示 小于1000,用g表示 计算属性 计算属性必须是有一个返回值的哦 在html写被计算的值 在computed中去直接调用哈 <view> <t ...

  8. git创建分支出现 fatal: Not a valid object name: 'master'.

    今天使用git 创建新分支的时候 出现了一个错误 fatal: Not a valid object name: 'master'. 问题描述:一个非法的master,原因:本地还没有创建master ...

  9. CF676C 题解

    使用尺取法(双指针法). 由于字符种类只有 \(2\) 种,答案一定是全 a 或全 b. 情况 \(1\):全 a 快指针循环移动,并统计字符 b 的数量 \(cntb\),直到 \(cntb\) 即 ...

  10. 大数据面试题集锦-Hadoop面试题(二)-HDFS

    你准备好面试了吗?这里有一些面试中可能会问到的问题以及相对应的答案.如果你需要更多的面试经验和面试题,关注一下"张飞的猪大数据分享"吧,公众号会不定时的分享相关的知识和资料. 目录 ...