线程池创建简单理解
如果还没有学习Runnable多线程,请先看我另外一篇Runnable的文章
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class cachethreadpool {
public static void main (String[] args) {
ExecutorService es = Executors.newFixedThreadPool(3);
int setup = 1;
for (int i = 0; i < setup; i++) { //for循环开始
final int index = i;
setup++;
es.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println(index);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
} //for循环结束
}
}
(个人理解)步骤:
- //新建ExecutorService(线程池)
ExecutorService es = new Executors.newFixedThreadPool(3); //线程池大小为3,所以创建三个线程
- //用ExecutorService的execute方法执行线程池中的线程
es.execute(new Runnable() { //新建Runnable
@Override
public void run() { //重写run方法(Runnable)
try {
System.out.println(index);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}); //好像还挺简单的…
深入理解
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class cachethreadpool {
public static void main (String[] args) {
ExecutorService es = Executors.newFixedThreadPool(3);
Prints pts = new Prints(1);
while (true) {
es.execute(pts);
}
/*Prints pts = new Prints(1);
pts.run();*/
}
}
class Prints implements Runnable {
int thnum;
public Prints(int thnum) {
this.thnum = thnum;
}
public void run() {
Long time;
Long time2;
Long time3;
time = System.currentTimeMillis();
for (int i = 0; i < 20; i++) {
try {
System.out.println("线程" + thnum + " NO." + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
time2 = System.currentTimeMillis();
time3 = time2 - time;
System.out.println(time3);
}
}
注意:给es.execute加了一个for循环,实际上在执行
ExecutorService es = Executors.newFixedThreadPool(3);
的时候,es就已经开始多线程线程了,而execute实际上可以指定执行很多个Runnable任务(class),只有一个限制,就是一个周期只能运行3个。
修改一下代码,再次运行尝试:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class cachethreadpool {
public static void main (String[] args) {
ExecutorService es = Executors.newFixedThreadPool(3);
Prints pts = new Prints(1);
Prints2 pts2 = new Prints2(2);
while (true) {
es.execute(pts);
es.execute(pts2);
}
/*Prints pts = new Prints(1);
pts.run();*/
}
}
class Prints implements Runnable {
int thnum;
public Prints(int thnum) {
this.thnum = thnum;
}
public void run() {
Long time;
Long time2;
Long time3;
time = System.currentTimeMillis();
for (int i = 0; i < 20; i++) {
try {
System.out.println("线程" + thnum + " NO." + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
time2 = System.currentTimeMillis();
time3 = time2 - time;
System.out.println(time3);
}
}
class Prints2 implements Runnable {
int thnum;
public Prints2(int thnum) {
this.thnum = thnum;
}
public void run() {
Long time;
Long time2;
Long time3;
time = System.currentTimeMillis();
for (int i = 0; i < 20; i++) {
try {
System.out.println("线程" + thnum + " NO." + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
time2 = System.currentTimeMillis();
time3 = time2 - time;
System.out.println(time3);
}
}
运行结果:
线程2 NO.0
线程1 NO.0
线程1 NO.0
线程1 NO.1
线程1 NO.1
线程2 NO.1
线程1 NO.2
线程2 NO.2
线程1 NO.2
线程1 NO.3
线程1 NO.3
线程2 NO.3
注意:运行结果每1秒会出现3个(因为有个sleep),而按照什么顺序执行实际上是看pts和pts2两个线程哪个先抢到cpu的资源。
其它线程池…
Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。 弹性线程池,无上限线程数(但要小心因为线程太多而导致的崩溃)
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。 推荐,一次只能运行特定数量的线程
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。