从JDK1.5之后的版本,加入了java.util.concurentt包,其中有java.util.concurrent.Executors和java.util.concurrent.ExecutorService实现线程池操作。线程池可以更加有效地管理线程,避免对资源的浪费和线程过多造成的拥挤。
线程池的顶级接口是Executors。严格得说Executors并不是实际的接口,而是为了实现线程池的工具,真正的接口是ExecutorService
要实现线程池是很不容易的,因此JAVA提供了静态工厂,生成一些常用的线程池。
1、newCachedThreadPool :创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
2、newFixedThreadPool :创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
3、newSheduledThreadPool :创建一个定长线程池,支持定时及周期性任务执行。
4、newSingledThreadExecutor :创建唯一的线程,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行
(1)newCachedThreadPool
<span style="font-size:18px;">import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Problem5test {
public static void main(String[] args) {
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int index = i;
try {
Thread.sleep(index * 1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
cachedThreadPool.execute(
new Runnable() {
public void run() {
System.out.println(index);
}
}
);
}
}
}
</span>
线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。(2)newFixedThreadPool
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Problem5test {
public static void main(String[] args) {
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);//定下是3个线程
for (int i = 0; i < 10; i++) {
final int index = i;
fixedThreadPool.execute( new Runnable() {
public void run() {
try {
System.out.println(index);
Thread.sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}
因为线程池大小为3,每个任务输出index后sleep 2秒,所以每两秒打印3个数字。
定长线程池的大小最好根据系统资源进行设置。如Runtime.getRuntime().availableProcessors()
(3)newScheduledThreaPool
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Problem5test {
public static void main(String[] args) {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
scheduledThreadPool.schedule(new Runnable() {
public void run() {
System.out.println("delay 3 seconds");
}
}, 3, TimeUnit.SECONDS);
}
}
延迟3秒执行run();
(4)newSingledTheadExecutor
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Problem5test {
public static void main(String[] args) {
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {
public void run() {
try {
System.out.println(index);
Thread.sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}
每2秒执行一次。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Problem5test {
public static void main(String[] args) {
ExecutorService singleThreadExecutor = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {
public void run() {
try {
while(true) {
System.out.println(index);
Thread.sleep(10 * 1000);
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}