---
title: "Concurrent - execute() & submit()"
author: "fairy_xzc"
date: "2016-12-08T13:06:29Z"
tags: []
categories: ["编程语言"]
slug: "concurrent-execute-submit"
draft: false
lastmod: "2016-12-09T09:15:18Z"
id: 2343704
url: "https://agilestyle.iteye.com/blog/2343704"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2343704](/blog/2343704)**

 

## execute()

execute()没有返回值，在默认的情况下异常直接抛出，不能捕获，但可以通过自定义ThreadFactory的方式进行捕获

![](http://dl2.iteye.com/upload/attachment/0121/8404/15437eb5-48c6-3d3e-9d61-a6f3e46c0cad.png)  
 

## submit()

submit()有返回值，在默认的情况下，可以catch ExecutionException捕获异常

![](http://dl2.iteye.com/upload/attachment/0121/8406/7de0d9e2-8842-35a9-ac9f-1e7170094205.png)  
 

### execute无返回值，submit有返回值

FutureCallableTest7.java

 

```
package org.fool.java.concurrent.futurecallable;

import java.util.concurrent.*;

public class FutureCallableTest7 {
    public static void main(String[] args) {
        try {
            ExecutorService executor = new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>());

            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("execute() invoked..., no return value...");
                }
            });

            Future<String> future = executor.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    System.out.println("submit() invoked..., has return value...");
                    return "data";
                }
            });

            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
```

Run

 

![](http://dl2.iteye.com/upload/attachment/0121/8408/f250ea63-55f3-3c5f-9e9e-505601234405.png)  
 

### execute出现异常后直接打印堆栈信息

FutureCallableTest8.java

```
package org.fool.java.concurrent.futurecallable;

import java.util.concurrent.*;

public class FutureCallableTest8 {
    public static void main(String[] args) {
        ExecutorService executor = new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>());

        executor.execute(new Runnable() {
            @Override
            public void run() {
                Integer.parseInt("a");
            }
        });
    }
}
```

Run

![](http://dl2.iteye.com/upload/attachment/0121/8410/466f6d19-2410-37f9-a95f-859e48d2a43c.png)  
 

### submit()可以捕获异常

FutureCallableTest9.java

```
package org.fool.java.concurrent.futurecallable;

import java.util.concurrent.*;

public class FutureCallableTest9 {
    public static void main(String[] args) {
        try {
            ExecutorService executor = new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>());

            Future<String> future = executor.submit(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    Integer.parseInt("a");
                    return "data exception";
                }
            });

            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            System.out.println("ExecutionException catched...");
            e.printStackTrace();
        }
    }
}
```

Run

![](http://dl2.iteye.com/upload/attachment/0121/8412/d7df2bf3-e20c-39e4-a222-69c601ff1248.png)  
 

### execute()自定义ThreadFactory的方式进行捕获异常

FutureCallableTest10.java

```
package org.fool.java.concurrent.futurecallable;

import java.util.concurrent.*;

public class FutureCallableTest10 {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>());

        executor.setThreadFactory(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);

                t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        System.out.println("Custom Exception in execute()");
                        e.printStackTrace();
                    }
                });

                return t;
            }
        });

        executor.execute(new Runnable() {
            @Override
            public void run() {
                Integer.parseInt("a");
            }
        });
    }
}
```

Run

![](http://dl2.iteye.com/upload/attachment/0121/8415/59807fc2-15cf-3390-82c4-82ddf999eed3.png)