线程池中exceute和submit的区别

简介

本次只说线程池中如何使用submit和execute,以及部分源码。
简单的说execute只能接受Runable的为参数,submit即可用接受Runable也可以接受Callabe为参数,并且都可以返回一个Future对象(ps:其实是一个RunnableFuture对象准确的说)。

execute

这个就是接受个Runnable参数。可以自己写个main方法看下。

submit

直接上代码运行就知道啥意思了
实现Callable的类

package com.test.multithread;

import java.util.concurrent.Callable;

/**
 * @Package: com.test.multithread
 * @ClassName: CallableInfo
 * @Description: 类描述
 * @Author: bulingfeng
 * @CreateDate: 2019/3/22/022 12:03
 */
public class CallableInfo implements Callable{
    @Override
    public Object call() throws Exception {
        Thread.sleep(1000);
        return "hello-callable";
    }
}

实现Runnable的类

package com.test.multithread;

/**
 * @Package: com.test.multithread
 * @ClassName: RunableInfo
 * @Description: 类描述
 * @Author: bulingfeng
 * @CreateDate: 2019/3/22/022 13:28
 */
public class RunableInfo implements Runnable{
    @Override
    public void run() {
        System.out.println("runable thread is running");
    }
}

测试submit提交callable的方法

package com.test.multithread;

import java.util.concurrent.*;

/**
 * @Package: com.test.multithread
 * @ClassName: ThreadPoolMain
 * @Description: 类描述
 * @Author: bulingfeng
 * @CreateDate: 2019/3/22/022 13:25
 */
public class ThreadPoolMain {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool= Executors.newFixedThreadPool(10);
        for (int i=0;i<100;i++) {
            CallableInfo callableInfo=new CallableInfo();
            Future futrue=pool.submit(callableInfo);
            ThreadPoolExecutor threadPoolExecutor= (ThreadPoolExecutor) pool;
            System.out.println("线程的队列数为:"+threadPoolExecutor.getQueue().size());
            System.out.println("PoolSize:"+threadPoolExecutor.getPoolSize());
            System.out.println("ActiveCount:"+threadPoolExecutor.getActiveCount());
            System.out.println(futrue.get());
        }
    }
}

可以知道submit提交能返回来一个Future。从而获取线程的执行结果。

submit也可以提交Runable。

package com.test.multithread;

import java.util.concurrent.*;

/**
 * @Package: com.test.multithread
 * @ClassName: ThreadPoolMain
 * @Description: 类描述
 * @Author: bulingfeng
 * @CreateDate: 2019/3/22/022 13:25
 */
public class ThreadPoolMain {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool= Executors.newFixedThreadPool(10);
        RunableInfo runableInfo=new RunableInfo();
        Future future=pool.submit(runableInfo,"hello");
        System.out.println(future.get());
    }
}

源码分析

根据代码可以发现submit只是ExecutorService接口的一个方法。并且发现也没有在ThreadPoolExecutor类中做实现。ThreadPoolExecutor继承AbstractExecutorService。
里面分别重写了submit和execute方法。
如下

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

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

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

就可以看到实际上市返回的一个RunableFuture。只不过RunableFuture实现了Future而已。
ps:如有问题,及时留言交流。

你可能感兴趣的:(线程池,submit,execute,java多线程)