[b]1、newCachedThreadPool[/b]
2、newFixedThreadPool
[b]3、newScheduledThreadPool[/b]
[b]4、newSingleThreadExecutor[/b]
package com.vxichina.cluster.persistence.thread.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 第一种线程池newCachedThreadPool
* 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
* @author admin
*
*/
public class ThreadPoolOne {
public static void main(String[] args) {
ExecutorService threadPool=Executors.newCachedThreadPool();//理论上有无限个线程,实际使用时会重复利用已有的空闲线程
//for #s
for (int i = 0; i < 100; i++) {
final int index=i;
//System.out.println("for循环中index的值="+index);
try {
Thread.sleep(i*100);//触发操作系统立刻重新进行一次CPU竞争
} catch (InterruptedException e) {
e.printStackTrace();
}
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("index的值="+index+", 当前线程:id="+Thread.currentThread().getId()+", name="+Thread.currentThread().getName());
}
});
}
//for #e
}
}
2、newFixedThreadPool
package com.vxichina.cluster.persistence.thread.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 第一种线程池newFixedThreadPool
* 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
* @author admin
*
*/
public class ThreadPoolTwo {
public static void main(String[] args) {
ExecutorService threadPool=Executors.newFixedThreadPool(3);//只设置3个线程
//for #s
for (int i = 0; i < 1000; i++) {
final int index=i;
//System.out.println("for循环中index的值="+index);
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("index的值="+index+", 当前线程:id="+Thread.currentThread().getId()+", name="+Thread.currentThread().getName());
try {
Thread.sleep(3000);//触发操作系统立刻重新进行一次CPU竞争
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
//for #e
}
}
[b]3、newScheduledThreadPool[/b]
package com.vxichina.cluster.persistence.thread.pool;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 第一种线程池newScheduledThreadPool
* 创建一个定长线程池,支持定时及周期性任务执行。
* @author admin
*
*/
public class ThreadPoolThree {
public static void main(String[] args) {
//ScheduledExecutorService比Timer更安全,功能更强大
ScheduledExecutorService threadPool=Executors.newScheduledThreadPool(3);//只设置3个线程
//#s
threadPool.schedule(new Runnable() {
@Override
public void run() {
System.out.println("我是延迟5秒后执行的哦");
}
}, 5, TimeUnit.SECONDS);//表示延迟5秒执行
//#e
//#s
threadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("我是延迟5秒后每2秒执行一回"+Calendar.getInstance().getTime().getHours()+":"+Calendar.getInstance().getTime().getMinutes()+":"+Calendar.getInstance().getTime().getSeconds());
}
}, 5, 2, TimeUnit.SECONDS);//延迟5秒后每2秒执行一次
//#e
//#s
threadPool.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("输出"+Calendar.getInstance().getTime().getHours()+":"+Calendar.getInstance().getTime().getMinutes()+":"+Calendar.getInstance().getTime().getSeconds());
}
}, 2, 5, TimeUnit.SECONDS);//初始延迟2秒后面每个延迟5秒执行
//#e
}
}
[b]4、newSingleThreadExecutor[/b]
package com.vxichina.cluster.persistence.thread.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 第一种线程池newSingleThreadExecutor
* 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
* @author admin
*
*/
public class ThreadPoolFour {
public static void main(String[] args) {
ExecutorService threadPool=Executors.newSingleThreadExecutor();//单一线程,相当于顺序执行各个任务
//for #s
for (int i = 0; i < 1000; i++) {
final int index=i;
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("index的值="+index+", 当前线程:id="+Thread.currentThread().getId()+", name="+Thread.currentThread().getName());
try {
Thread.sleep(1000);//触发操作系统立刻重新进行一次CPU竞争
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//for #e
}
}