第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 {@Overridepublic void run() {int result;// Create a random number generatorRandom random=new Random(Thread.currentThread().getId());while (true) {// Generate a random number a calculate 1000 divide by that random numberresult=1000/((int)(random.nextDouble()*1000));System.out.printf("%s : %f\n",Thread.currentThread().getId(),result);// Check if the Thread has been interruptedif (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 */@Overridepublic void uncaughtException(Thread t, Throwable e) {// Prints the name of the ThreadSystem.out.printf("The thread %s has thrown an Exception\n",t.getId());// Print the stack trace of the exceptione.printStackTrace(System.out);// Interrupt the rest of the threads of the thread groupSystem.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 objectMyThreadGroup threadGroup=new MyThreadGroup("MyThreadGroup");// Create a Taks objectTask task=new Task();// Create and start two Thread objects for this Taskfor (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