zoukankan      html  css  js  c++  java
  • 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,也可以为Runnable。

    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);
      ...
    }
    
  • 相关阅读:
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)(A-C题解)
    最小生成树
    并查集
    字符串的匹配算法
    CTU Open Contest 2019 AB题
    Codeforces Round #624 (Div. 3)(题解)
    Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)
    Lyft Level 5 Challenge 2018
    终于在博客写完第一篇题解啦
    Codeforces Round #519 by Botan Investments(前五题题解)
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/9784821.html
Copyright © 2011-2022 走看看