package com.concurrent;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ExecutorsTest {
/**
* 老办法
* */
public static void test1() {
// 开20个线程
for (int i=0;i<20;i++) {
Runnable thread = new ExecutorsTest().new OneThread();
new Thread(thread).start();
}
}
public static void test2(){
// 创建一个固定数目线程的线程池
ExecutorService service = Executors.newFixedThreadPool(5);
// 同下
/*
ExecutorService service2 = new ThreadPoolExecutor
(5,Integer.MAX_VALUE,0,TimeUnit.MILLISECONDS,new LinkedBlockingQueue
*/
// 始终是上面创建的5个线程的运行
for (int i=0;i<10;i++) {
Runnable thread = new ExecutorsTest().new OneThread();
service.execute(thread);
}
service.shutdown();
}
public static void test3() {
/**
* 创建一个可缓存的线程池,调用execute() 将重用以前构造的线程(可用线程);
* 如果现有线程没有可用的,则创建一个新的线程添加到线程池中;
* 终止并从缓存中移除哪些 60秒之后没有使用的线程
* */
//ExecutorService service = Executors.newCachedThreadPool();
/**
* 上面的60秒改成2秒
* */
ExecutorService service = new ThreadPoolExecutor
(0,Integer.MAX_VALUE,2,TimeUnit.SECONDS,new SynchronousQueue
for (int i=0;i<20;i++) {
Runnable thread = new ExecutorsTest().new OneThread();
service.execute(thread);
try {
Thread.sleep(1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
service.shutdown();
}
public static void test4() {
/**
* 创建一个单线程化得Executors
* */
ExecutorService service = Executors.newSingleThreadExecutor();
/*
ExecutorService service2 = new ThreadPoolExecutor
(1,1,0,TimeUnit.MILLISECONDS,new LinkedBlockingQueue
*/
for (int i=0;i<10;i++) {
Runnable thread = new ExecutorsTest().new OneThread();
service.execute(thread);
}
service.shutdown();
}
public static void test5() {
/**
* 创建一个支持定时和周期性任务的Executors
* */
ExecutorService service = Executors.newScheduledThreadPool(5);
for (int i=0;i<20;i++) {
Runnable thread = new ExecutorsTest().new OneThread();
service.execute(thread);
}
service.shutdown();
}
public static void main(String[] args) {
test3();
}
public static void testScheduledExecutorService() {
final ScheduledExecutorService service = Executors.newScheduledThreadPool(5);
// initialDelay秒后开始执行,每隔period秒执行一次 period必须大于0
service.scheduleAtFixedRate(new Runnable(){
public void run() {
System.out.println(Thread.currentThread().getName()+" AtFix "+":"+new Date());
if (i > 5) {
Integer.parseInt("e");
//service.shutdown();
}
i++;
}
}, 0, 1, TimeUnit.SECONDS);
// initialDelay秒后开始执行,一次执行终止和下一次执行开始之间的延迟period秒
service.scheduleWithFixedDelay(new Runnable(){
public void run() {
System.out.println(Thread.currentThread().getName()+" withFix "+":"+new Date());
if (i > 10) {
service.shutdown();
}
i++;
}
}, 2, 3, TimeUnit.SECONDS);
try {
//Thread.sleep(20000);
//service.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 内部类
* */
public class OneThread implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName()+" : "+new Date().getTime());
}
}
}