Java concurrent Programming instance--11. Processing of unchecked exceptions in thread groups __java core

Source: Internet
Author: User

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






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.