Java並發編程執行個體--11.線上程組中處理未檢查異常__Java核心

來源:互聯網
上載者:User

第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






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.