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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构