线程池

作者: 秋秋_6403 | 来源:发表于2020-05-13 08:38 被阅读0次
image

不要忘记关闭服务

execute,执行实现runnable接口的对象

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

public class TestThreadPool implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());

    }

    public static void main(String[] args) {

        ExecutorService service = Executors.newFixedThreadPool(3);
        service.execute(new TestThreadPool());
        service.execute(new TestThreadPool());
        service.execute(new TestThreadPool());
        service.shutdown();

    }
}

submit,执行实现callable的对象

import util.WebDownloader;

import javax.sql.rowset.CachedRowSet;
import java.util.concurrent.*;

public class TestCallable implements Callable {

    private String url;
    private String name;

    public TestCallable(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public Integer call() throws Exception {
        WebDownloader downloader = new WebDownloader();
        downloader.download(url, name);
        System.out.println(name + "下载成功");

        return 1;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable callable = new TestCallable("http://www.baidu.com/img/bd_logo1.png", "image1.jpg");
        TestCallable callable2 = new TestCallable("http://www.baidu.com/img/bd_logo1.png", "image2.jpg");

        TestCallable callable3 = new TestCallable("http://www.baidu.com/img/bd_logo1.png", "image3.jpg");

        //创建执行服务
        ExecutorService service = Executors.newFixedThreadPool(3);
        //提交执行,相当于start()
        Future<Integer> result1 = service.submit(callable);
        Future<Integer> result2 = service.submit(callable2);
        Future<Integer> result3 = service.submit(callable3);
        //获取结果
        System.out.println(result1.get());
        System.out.println(result2.get());
        System.out.println(result3.get());
        //关闭服务
        service.shutdown();


    }
}

相关文章

  • java线程池

    线程VS线程池 普通线程使用 创建线程池 执行任务 执行完毕,释放线程对象 线程池 创建线程池 拿线程池线程去执行...

  • java----线程池

    什么是线程池 为什么要使用线程池 线程池的处理逻辑 如何使用线程池 如何合理配置线程池的大小 结语 什么是线程池 ...

  • Java线程池的使用

    线程类型: 固定线程 cached线程 定时线程 固定线程池使用 cache线程池使用 定时调度线程池使用

  • Spring Boot之ThreadPoolTaskExecut

    初始化线程池 corePoolSize 线程池维护线程的最少数量keepAliveSeconds 线程池维护线程...

  • 线程池

    1.线程池简介 1.1 线程池的概念 线程池就是首先创建一些线程,它们的集合称为线程池。使用线程池可以很好地提高性...

  • 多线程juc线程池

    java_basic juc线程池 创建线程池 handler是线程池拒绝策略 排队策略 线程池状态 RUNNIN...

  • ThreadPoolExecutor线程池原理以及源码分析

    线程池流程: 线程池核心类:ThreadPoolExecutor:普通的线程池ScheduledThreadPoo...

  • 线程池

    线程池 [TOC] 线程池概述 什么是线程池 为什么使用线程池 线程池的优势第一:降低资源消耗。通过重复利用已创建...

  • java 线程池使用和详解

    线程池的使用 构造方法 corePoolSize:线程池维护线程的最少数量 maximumPoolSize:线程池...

  • 线程池

    JDK线程池 为什么要用线程池 线程池为什么这么设计 线程池原理 核心线程是否能被回收 如何回收空闲线程 Tomc...

网友评论

      本文标题:线程池

      本文链接:https://www.haomeiwen.com/subject/hgegnhtx.html