线程池第一次使用,我写的跟弱智一样

本文介绍了Java中使用ExecutorService接口和Executors类创建线程池的实例。通过ExecutorService的execute方法启动任务,并展示了如何通过submit获取Future结果。最后,调用shutdown方法关闭线程池。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package ThreadTest.Day3;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author CCQ
 * 线程池
 * ExecutorService是一个线程池接口
 * Executors是一个实现类。有一个静态方法newFixedThreadPool(int a)实现创建线程池,参数为池子大小
 */
public class Test08 {
    public static void main(String[] args) {
        //创建服务。创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        //执行,这个方法没有返回值
        executorService.execute(new Mythread());
        executorService.execute(new Mythread());
        executorService.execute(new Mythread());

        //也可以用submit执行。submit有返回值
        Future<?> submit = executorService.submit(new Mythread());

        //关闭链接
        executorService.shutdown();
    }
}


class Mythread implements Runnable{
    @Override
    public void run() {
            System.out.println(Thread.currentThread().getName());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值