线程池中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<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;
    }

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值