java-forkjoin異常返回

來源:互聯網
上載者:User

標籤:log   eve   dex   time   style   cut   second   interrupt   printf   

來自:http://ifeve.com/fork-join-5/

在Java中有兩種異常:

  • 已檢查異常(Checked exceptions):這些異常必須在一個方法的throws從句中指定或在內部捕捉它們。比如:IOException或ClassNotFoundException。
  • 未檢查異常(Unchecked exceptions):這些異常不必指定或捕捉。比如:NumberFormatException。

在ForkJoinTask類的compute()方法中,你不能拋出任何已檢查異常,因為在這個方法的實現中,它沒有包含任何拋出(異常)聲明。你必須包含必要的代碼來處理異常。但是,你可以拋出(或者它可以被任何方法或使用內部方法的對象拋出)一個未檢查異常。ForkJoinTask和ForkJoinPool類的行為與你可能的期望不同。程式不會結束執行,並且你將不會在控制台看到任何關於異常的資訊。它只是被吞沒,好像它沒拋出(異常)。你可以使用ForkJoinTask類的一些方法,得知一個任務是否拋出異常及其異常種類。在這個指南中,你將學習如何擷取這些資訊。

task: 

package com.wenbronk.forkjoin.exception;import java.util.concurrent.RecursiveTask;import java.util.concurrent.TimeUnit;/** * forkjoin 中拋出異常的處理 * Created by wenbronk on 2017/7/27. */public class Task extends RecursiveTask<Integer> {    private int array[];    private int start, end;    public Task(int[] array, int start, int end) {        this.array = array;        this.start = start;        this.end = end;    }    @Override    protected Integer compute() {        System.out.printf("task: start from %d to %d \n", start, end);        if (end - start < 10) {            if ((3 > start) && (3 < end)) {                System.out.println("paochu yichang " + start + " to " + end);                throw new RuntimeException("task from " + start + " to " + end);            }            try {                TimeUnit.SECONDS.sleep(1);            }catch (Exception e) {                e.printStackTrace();            }        }else {            int mid = (start + end) /2;            Task task1 = new Task(array, start, mid);            Task task2 = new Task(array, mid, end);            invokeAll(task1, task2);        }        System.out.printf("task: end from %d to %d \n", start, end);        return 0;    }}

main: 

package com.wenbronk.forkjoin.exception;import java.util.concurrent.ForkJoinPool;import java.util.concurrent.TimeUnit;/** * Created by wenbronk on 2017/7/27. */public class ExceptionMain {    public static void main(String[] args) {        int[] array = new int[100];        Task task = new Task(array, 0, 100);        ForkJoinPool forkJoinPool = new ForkJoinPool();        forkJoinPool.execute(task);        forkJoinPool.shutdown();        try {            forkJoinPool.awaitTermination(1, TimeUnit.DAYS);        } catch (InterruptedException e) {            e.printStackTrace();        }        if (task.isCompletedAbnormally()) {            System.out.println("main::: an exception has occured \n");            System.out.printf("main:::: %s \n", task.getException());        }        System.out.printf("main: result: %d", task.join());    }}

 

java-forkjoin異常返回

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.