最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

簡單解析execute和submit有什么區(qū)別

 更新時(shí)間:2020年11月10日 10:25:25   作者:牛鼻子老趙  
這篇文章主要介紹了簡單解析execute和submit有什么區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、execute 方法位于 java.util.concurrent.Executor 中

void execute(Runnable command);

2、execute 的具體實(shí)現(xiàn)

public void execute(Runnable command) {
    if (command == null)
      throw new NullPointerException();
    /*
     * Proceed in 3 steps:
     *
     * 1. If fewer than corePoolSize threads are running, try to
     * start a new thread with the given command as its first
     * task. The call to addWorker atomically checks runState and
     * workerCount, and so prevents false alarms that would add
     * threads when it shouldn't, by returning false.
     *
     * 2. If a task can be successfully queued, then we still need
     * to double-check whether we should have added a thread
     * (because existing ones died since last checking) or that
     * the pool shut down since entry into this method. So we
     * recheck state and if necessary roll back the enqueuing if
     * stopped, or start a new thread if there are none.
     *
     * 3. If we cannot queue task, then we try to add a new
     * thread. If it fails, we know we are shut down or saturated
     * and so reject the task.
     */
    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);
  }

3、submit 方法位于 java.util.concurrent.AbstractExecutorService 中

/**
   * @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;
  }

4、submit 方式使用 Runnable 入?yún)r(shí)的具體實(shí)現(xiàn)

static final class RunnableAdapter<T> implements Callable<T> {
    final Runnable task;
    final T result;
    RunnableAdapter(Runnable task, T result) {
      this.task = task;
      this.result = result;
    }
    public T call() {
      task.run();
      return result;
    }
  }

5、submit 方式使用 Callable 入?yún)r(shí)的具體實(shí)現(xiàn)

public FutureTask(Callable<V> callable) {
    if (callable == null)
      throw new NullPointerException();
    this.callable = callable;
    this.state = NEW;    // ensure visibility of callable
  }

  //重寫run方法
  public void run() {
    if (state != NEW ||
      !UNSAFE.compareAndSwapObject(this, runnerOffset,
                     null, Thread.currentThread()))
      return;
    try {
      Callable<V> c = callable;
      if (c != null && state == NEW) {
        V result;
        boolean ran;
        try {
          result = c.call();
          ran = true;
        } catch (Throwable ex) {
          result = null;
          ran = false;
          setException(ex);
        }
        if (ran)
          set(result);
      }
    } finally {
      // runner must be non-null until state is settled to
      // prevent concurrent calls to run()
      runner = null;
      // state must be re-read after nulling runner to prevent
      // leaked interrupts
      int s = state;
      if (s >= INTERRUPTING)
        handlePossibleCancellationInterrupt(s);
    }
  }

總結(jié):

1、根據(jù)源碼可以看到 execute 僅可以接受Runnable類型,而 submit 重載了三個(gè)方法,參數(shù)可以是 Runnable 類型、Runnable 類型+泛型T 、Callable 類型接口。

2、從上面源碼可以看出 submit 方法實(shí)際上如果用Runnable類型的接口可以有返回值,也可以沒有返回值。

3、傳遞Runnable類型接口加泛型T會(huì)被進(jìn)一步封裝,在 Executors 這個(gè)類里面有個(gè)內(nèi)部類 RunnableAdapter 實(shí)現(xiàn)了 Callable 接口。

4、看submit方法可以看出,submit最終也是在調(diào)用 execute 方法,無論是 Runnable 還是 Callable 類型接口,都會(huì)被封裝成 FutureTask 繼續(xù)執(zhí)行。

5、如果使用submit方法提交,會(huì)進(jìn)一步封裝成FutureTask,執(zhí)行execute方法,在FutureTask里面重寫的run方法里面調(diào)用 Callable 接口的call方法。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用SpringBoot跨系統(tǒng)調(diào)用接口的方案

    使用SpringBoot跨系統(tǒng)調(diào)用接口的方案

    這篇文章主要介紹了使用SpringBoot跨系統(tǒng)調(diào)用接口的方案,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Spring中的AutowireCandidateResolver的具體使用詳解

    Spring中的AutowireCandidateResolver的具體使用詳解

    這篇文章主要介紹了Spring中的AutowireCandidateResolver的具體使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • java實(shí)現(xiàn)簡單驗(yàn)證碼生成

    java實(shí)現(xiàn)簡單驗(yàn)證碼生成

    這篇文章主要介紹了java實(shí)現(xiàn)簡單驗(yàn)證碼生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • SpringSecurity之SecurityContextHolder使用解讀

    SpringSecurity之SecurityContextHolder使用解讀

    這篇文章主要介紹了SpringSecurity之SecurityContextHolder使用解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • mybatis中xml之trim屬性說明

    mybatis中xml之trim屬性說明

    這篇文章主要介紹了mybatis中xml之trim屬性說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java根據(jù)日期截取字符串的多種實(shí)現(xiàn)方法

    Java根據(jù)日期截取字符串的多種實(shí)現(xiàn)方法

    在實(shí)際開發(fā)中,我們經(jīng)常會(huì)遇到需要根據(jù)日期來截取字符串的需求,例如從文件名中提取日期信息,Java 提供了多種方法來實(shí)現(xiàn)根據(jù)日期來截取字符串的功能,本文將給大家介紹了Java根據(jù)日期截取字符串的多種實(shí)現(xiàn)方法,需要的朋友可以參考下
    2024-11-11
  • 詳解Java如何簡化條件表達(dá)式

    詳解Java如何簡化條件表達(dá)式

    在復(fù)雜的實(shí)際業(yè)務(wù)中,往往會(huì)出現(xiàn)各種嵌套的條件判斷邏輯。隨著需求的增加,條件邏輯會(huì)變得越來越復(fù)雜。面對這種情況,簡化判斷邏輯就是不得不做的事情,下面為大家介紹幾種方法
    2022-06-06
  • idea切換分支的時(shí)候,忽略一些無用的修改設(shè)置

    idea切換分支的時(shí)候,忽略一些無用的修改設(shè)置

    這篇文章主要介紹了idea切換分支的時(shí)候,忽略一些無用的修改操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 如何在mapper文件中使用in("str1","str2")

    如何在mapper文件中使用in("str1","str2")

    這篇文章主要介紹了如何在mapper文件中使用in("str1","str2"),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • idea如何debug看springsecurity的過濾器順序

    idea如何debug看springsecurity的過濾器順序

    這篇文章主要介紹了idea如何debug看springsecurity的過濾器順序,文中通過圖文結(jié)合的方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-04-04

最新評論

阿荣旗| 太康县| 临洮县| 兰考县| 威信县| 纳雍县| 元朗区| 建湖县| 德江县| 黑山县| 商丘市| 额尔古纳市| 潢川县| 靖宇县| 朝阳区| 田林县| 罗江县| 卢湾区| 鹤山市| 海盐县| 宁蒗| 大悟县| 平舆县| 遂平县| 蓬安县| 庐江县| 乌海市| 冀州市| 阿克苏市| 湖南省| 石城县| 措美县| 墨江| 林西县| 滨州市| 若尔盖县| 嘉定区| 临泽县| 雅江县| 康保县| 隆安县|