线程池基本使用

线程池基本使用

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

public class PoolDemo {
    public static void main(String[] args) {
        // 创建一个长度为5的固定线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        // 执行
        executorService.execute(new MyThread());
        executorService.execute(new MyThread());
        executorService.execute(new MyThread());
        executorService.execute(new MyThread());


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

// Runnable接口实现类
class MyThread implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
上一篇:利用PWM控制灯光


下一篇:关于 modbustcp python 借鉴别人的码