JDK1.5 线程池
Executors类主要方法:
a. static ExecutorService newCachedThreadPool()
产生一个ExecutorService对象,这个对象带有一个线程池,线程池的大小会根据需要调整,线程执行完任务后返回线程池,供执行下一次任务使用
b. static ExecutorService newFixedThreadPool(int poolSize)
产生一个ExecutorService对象,这个对象带有一个大小为poolSize的线程池,若任务数量大于poolSize,任务会被放在一个queue里顺序执行
c. static ExecutorService newSingleThreadExecutor()
产生一个ExecutorService对象,这个对象只有一个线程可用来执行任务,若任务多于一个,任务将按先后顺序执行。
一.static ExecutorService newFixedThreadPool(int poolSize):创建一个可重用固定线程集合的线程池,以共享的无界队列方式来运行这些线程。
实例1:
package com.bijian.thread;
public class MyThread extends Thread {
private String nickName;
public MyThread(String nickName) {
super();
this.nickName = nickName;
}
@Override
public void run() {
try {
Thread.sleep(50);
} catch(Exception e) {
}
System.out.println(Thread.currentThread().getName() + ": "+ nickName + "正在执行。。。");
try {
Thread.sleep(100);
} catch(Exception e) {
}
}
}
package com.bijian.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//创建一个可重用固定线程数的线程池
ExecutorService pool = Executors.newFixedThreadPool(2);
//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
Thread t1 = new MyThread("A");
Thread t2 = new MyThread("B");
Thread t3 = new MyThread("C");
Thread t4 = new MyThread("D");
Thread t5 = new MyThread("E");
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//pool.exe
//关闭线程池
pool.shutdown();
}
}
运行结果:
pool-1-thread-2: B正在执行。。。 pool-1-thread-1: A正在执行。。。 pool-1-thread-2: C正在执行。。。 pool-1-thread-1: D正在执行。。。 pool-1-thread-1: E正在执行。。。
二.static ExecutorServicenewSingleThreadExecutor():创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程。
实例2(修改Main类如下):
package com.bijian.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//创建一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程
ExecutorService pool = Executors.newSingleThreadExecutor();
//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
Thread t1 = new MyThread("A");
Thread t2 = new MyThread("B");
Thread t3 = new MyThread("C");
Thread t4 = new MyThread("D");
Thread t5 = new MyThread("E");
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//关闭线程池
pool.shutdown();
}
}
运行结果:
pool-1-thread-1: A正在执行。。。 pool-1-thread-1: B正在执行。。。 pool-1-thread-1: C正在执行。。。 pool-1-thread-1: D正在执行。。。 pool-1-thread-1: E正在执行。。。
三.static ExecutorServicenewCachedThreadPool():创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。
实例3(修改Main类如下):
package com.bijian.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们
ExecutorService pool = Executors.newCachedThreadPool();
//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
Thread t1 = new MyThread("A");
Thread t2 = new MyThread("B");
Thread t3 = new MyThread("C");
Thread t4 = new MyThread("D");
Thread t5 = new MyThread("E");
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
try {
Thread.sleep(4000);
} catch(Exception e) {
}
Thread t6 = new MyThread("F");
pool.execute(t6);
//关闭线程池
pool.shutdown();
}
}
运行结果:
pool-1-thread-1: A正在执行。。。 pool-1-thread-5: E正在执行。。。 pool-1-thread-4: D正在执行。。。 pool-1-thread-2: B正在执行。。。 pool-1-thread-3: C正在执行。。。 pool-1-thread-1: F正在执行。。。
四.static ScheduledExecutorServicenewScheduledThreadPool(int corePoolSize): 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。
ScheduledExecutorService接口方法:
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit):创建并执行在给定延迟后启用的一次性操作。
实例4(修改Main类如下):
package com.bijian.thread;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
Thread t1 = new MyThread("A");
Thread t2 = new MyThread("B");
Thread t3 = new MyThread("C");
Thread t4 = new MyThread("D");
Thread t5 = new MyThread("E");
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
//1000毫秒后执行t4线程
pool.schedule(t4, 1000, TimeUnit.MILLISECONDS);
//1000毫秒后执行t5线程
pool.schedule(t5, 1000, TimeUnit.MILLISECONDS);
//关闭线程池
pool.shutdown();
}
}
运行结果:
pool-1-thread-2: B正在执行。。。 pool-1-thread-1: A正在执行。。。 pool-1-thread-1: C正在执行。。。 pool-1-thread-2: D正在执行。。。 pool-1-thread-1: E正在执行。。。
五.static ScheduledExecutorServicenewSingleThreadScheduledExecutor():创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行。
实例5(修改Main类如下):
package com.bijian.thread;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行
ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
Thread t1 = new MyThread("A");
Thread t2 = new MyThread("B");
Thread t3 = new MyThread("C");
Thread t4 = new MyThread("D");
Thread t5 = new MyThread("E");
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.schedule(t4, 5000, TimeUnit.MILLISECONDS);
pool.schedule(t5, 1000, TimeUnit.MILLISECONDS);
//关闭线程池
pool.shutdown();
}
}
运行结果:
pool-1-thread-1: A正在执行。。。 pool-1-thread-1: B正在执行。。。 pool-1-thread-1: C正在执行。。。 pool-1-thread-1: E正在执行。。。 pool-1-thread-1: D正在执行。。。
六.ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) :用给定的初始参数和默认的线程工厂及处理程序创建新的 ThreadPoolExecutor。
实例6(修改Main类如下):
package com.bijian.thread;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
//创建等待队列
BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue<Runnable>(20);
//用给定的初始参数和默认的线程工厂及处理程序创建新的 ThreadPoolExecutor
ThreadPoolExecutor pool = new ThreadPoolExecutor(2,3,2,TimeUnit.MILLISECONDS,bqueue);
//创建实现了Runnable 接口对象,Thread 对象当然也实现了Runnable 接口
Thread t1 = new MyThread("A");
Thread t2 = new MyThread("B");
Thread t3 = new MyThread("C");
Thread t4 = new MyThread("D");
Thread t5 = new MyThread("E");
Thread t6 = new MyThread("F");
Thread t7 = new MyThread("G");
bqueue.add(t7);
bqueue.add(t2);
bqueue.add(t3);
bqueue.add(t1);
bqueue.add(t5);
bqueue.add(t6);
bqueue.add(t4);
bqueue.add(t1);
//关闭线程池
pool.shutdown();
}
}
运行结果(线程池依次执行队列中的线程对象):
pool-1-thread-1: G正在执行。。。 pool-1-thread-1: B正在执行。。。 pool-1-thread-1: C正在执行。。。 pool-1-thread-1: A正在执行。。。 pool-1-thread-1: E正在执行。。。 pool-1-thread-1: F正在执行。。。 pool-1-thread-1: D正在执行。。。 pool-1-thread-1: A正在执行。。。
JDK1.5自带线程池的好处:
a. 分配线程、线程使用完回收至线程池、线程池没有线程可分配等待这些工作都是自动完成的
b. 方法的操作非常简便