1.初识线程池:
根据系统自身的环境情况,有效的限制执行线程的数量,使得运行效果达到最佳。线程主要是通过控制执行的线程的数量,超出数量的线程排队等候,等待有任务执行完毕,再从队列最前面取出任务执行。
2.线程池作用:
减少创建和销毁线程的次数,每个工作线程可以多次使用
可根据系统情况调整执行的线程数量,防止消耗过多内存
3.使用
ExecutorService:线程池接口
ExecutorService pool = Executors.常见线程
eg:ExecutorService pool = Executors.newSingleThreadExecutor();
4.常见线程池
①newSingleThreadExecutor
单个线程的线程池,即线程池中每次只有一个线程工作,单线程串行执行任务
②newFixedThreadExecutor(n)
固定数量的线程池,没提交一个任务就是一个线程,直到达到线程池的最大数量,然后后面进入等待队列直到前面的任务完成才继续执行
③newCacheThreadExecutor(推荐使用)
可缓存线程池,当线程池大小超过了处理任务所需的线程,那么就会回收部分空闲(一般是60秒无执行)的线程,当有任务来时,又智能的添加新线程来执行。
④newScheduleThreadExecutor
大小无限制的线程池,支持定时和周期性的执行线程
演示代码
/**
* @author hejie
* @date 2018年8月6日
*线程池,可伸缩的线程池
*/
package ThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CatchThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int no=i;
Runnable runnable = new Runnable() {
public void run() {
try {
System.out.println("into"+no);
Thread.sleep(500L);
System.out.println("end"+no);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
executor.execute(runnable);
}
executor.shutdown();
System.out.println("Thread Main End");
}
}
结果
into0
into3
into2
into1
Thread Main End
into4
into5
into7
into8
into6
into9
end2
end4
end1
end3
end0
end7
end8
end6
end9
end5
/**
* @author hejie
* @date 2018年8月6日
*线程池,只有一个线程工作,且顺序是提交的顺序
*/
package ThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SingleThreadExectorDemo {
public static void main(String[] args) {
ExecutorService executor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final int no=i;
Runnable runnable = new Runnable() {
public void run() {
try {
System.out.println("into"+no);
Thread.sleep(500L);
System.out.println("end"+no);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
executor.execute(runnable);
}
executor.shutdown();
System.out.println("Thread Main End");
}
}
结果
Thread Main End
into0
end0
into1
end1
into2
end2
into3
end3
into4
end4
into5
end5
into6
end6
into7
end7
into8
end8
into9
end9
/**
* @author hejie
* @date 2018年8月6日
*线程池,固定大小
*/
package ThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FixThreadPoolDemo {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
final int no=i;
Runnable runnable = new Runnable() {
public void run() {
try {
System.out.println("into"+no);
Thread.sleep(500L);
System.out.println("end"+no);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
executor.execute(runnable);
}
executor.shutdown();
System.out.println("Thread Main End");
}
}
结果:
into1
Thread Main End
into4
into0
into3
into2
end0
end2
end1
into5
end4
into8
end3
into9
into7
into6
end6
end9
end8
end7
end5
先到这里吧,过两天再改。