关于java线程池的理解1


1、首先定义线程池允许的线程数

定义多个线程数并行的线程池如下,定义了两个线程并行的线程池:

ExecutorService excutorThreadPool = Executors.newFixedThreadPool(2);


定义单个线程运行的线程池:

ExecutorService excutorThreadPool = Executors.newSingleThreadExecutor();


2、定义一个启动线程的class类:

class ThreadTask implements Runnable{}

初始化

ThreadTask threadTask1 = new ThreadTask ("threadTask1);

ThreadTask threadTask2 = new ThreadTask ("threadTask1);

ThreadTask threadTask3 = new ThreadTask ("threadTask1);


3、将线程加入到线程池中进行运行线程:

excutorThreadPool.execute(threadTask1);

excutorThreadPool.execute(threadTask2);

excutorThreadPool.execute(threadTask3);

你可能感兴趣的:(关于java线程池的理解1)