线程池的使用

2021-03-18 03:24

阅读:698

标签:java   调用   ble   style   runnable   create   jdk   执行   使用   

线程池

jdk1.5后官方提供线程池


 

创建一个线程类

/**
 * @Classname runnabledemo
 * @Description TODO
 * @Date 2020/11/11 11:59
 * @Created by Administrator
 */
public class runnabledemo implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" 正在执行...");
    }
}

创建一个测试类

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

/**
 * @Classname demo01
 * @Description TODO
 * @Date 2020/11/11 10:14
 * @Created by Administrator
 */
public class demo01 {
    public static void main(String[] args) {
        //1.创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        //2.调用线程池中的线程执行指定的线程对象
        executorService.submit(new runnabledemo());
        executorService.submit(new runnabledemo());
        executorService.submit(new runnabledemo());
        //3.销毁线程池(不建议执行)
        executorService.shutdown();
    }
}

 

线程池的使用

标签:java   调用   ble   style   runnable   create   jdk   执行   使用   

原文地址:https://www.cnblogs.com/sun-10387834/p/13958066.html


评论


亲,登录后才可以留言!