The 8th example tells how to catch unchecked exceptions in a thread, and this example describes how to handle unchecked exceptions in a thread group.
Task.java
Package com.dylan.thread.ch1.c11.task;
Import Java.util.Random;
/**
* Class that implements the concurrent task * */public
Class Task implements Runnable {
@Overrid E 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 from that random number
result=1000/((int) (Rand Om.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); c12/>}
/**
* 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"); C24/>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 Mythrea Dgroup ("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 ();}}
Run Result:
10:11:the thread has thrown an Exception
The thread 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