面试之--Java线程池中submit()和execute之间的区别?

394 阅读7分钟
原文链接: www.cnblogs.com
西北野狼
我的github: https://github.com/soyoungboy
我的segmentfault: http://segmentfault.com/u/soyoungboy

【不积跬步,无以至千里;不积小流,无以成江海】
博客园   首页   新随笔   联系   订阅 订阅  管理

Java线程池中submit()和execute之间的区别?

一:
submit()方法,可以提供Future < T > 类型的返回值。
executor()方法,无返回值。

execute无返回值

public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();//抛掉异常
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

submit有Future返回值 :

/**
     * @throws RejectedExecutionException {@inheritDoc}
     * @throws NullPointerException       {@inheritDoc}
     */
    public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }

    /**
     * @throws RejectedExecutionException {@inheritDoc}
     * @throws NullPointerException       {@inheritDoc}
     */
    public <T> Future<T> submit(Runnable task, T result) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task, result);
        execute(ftask);
        return ftask;
    }

    /**
     * @throws RejectedExecutionException {@inheritDoc}
     * @throws NullPointerException       {@inheritDoc}
     */
    public <T> Future<T> submit(Callable<T> task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }

二:
excute方法会抛出异常。
sumbit方法不会抛出异常。除非你调用Future.get()。


三:
excute入参Runnable
submit入参可以为Callable

public interface Executor {
    void execute(Runnable command);
}
public interface ExecutorService extends Executor {
  ...
  <T> Future<T> submit(Callable<T> task);
 
  <T> Future<T> submit(Runnable task, T result);
 
  Future<?> submit(Runnable task);
  ...
}
posted on 2018-10-13 23:49 西北野狼 阅读(60) 评论(0) 编辑 收藏 刷新评论刷新页面返回顶部 注册用户登录后才能发表评论,请 登录注册访问网站首页。 最新IT新闻:
· 每年拯救50万人的性命,人类离消灭疟疾还有多远?
· 小声比比:我承认,我们一二三四,四个人是有组织攻击马蜂窝的!
· 专访港珠澳大桥总工程师林鸣、岛隧总设计师、总设计师孟凡超
· 港珠澳大桥就要通车了,它都用到了哪些顶尖科技?
· Linus Torvalds重新负责内核开发
» 更多新闻... 最新知识库文章:
· 阿里云的这群疯子
· 为什么说 Java 程序员必须掌握 Spring Boot ?
· 在学习中,有一个比掌握知识更重要的能力
· 如何招到一个靠谱的程序员
· 一个故事看懂“区块链”
» 更多知识库文章... 历史上的今天:
2017-10-13 JdbcTemplate的使用
昵称:西北野狼
园龄:5年2个月
粉丝:53
关注: 4 +加关注
< 2018年10月 >
30 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
4 5 6 7 8 9 10

搜索

   

常用链接

我的标签

随笔分类

随笔档案

My github

我的个人博客站点

积分与排名

  • 积分 - 156573
  • 排名 - 2074

最新评论

阅读排行榜

评论排行榜

推荐排行榜

Powered by: 博客园 模板提供:沪江博客 Copyright ©2018 西北野狼