线程池的使用
2014-05-10 23:28
316 查看
一、使用Executors来创建简单线程池
1、ExecutorService pools = Executors.newCachedThreadPool();
2、ExecutorService pools = Executors.newSingleThreadExecutor();
3、ExecutorService pools = Executors.newFixedThreadPool(3);
每种的不同点看JDK的API
二、使用Executors来创建Scheduled线程池
1、ScheduledExecutorService pools = Executors.newSingleThreadScheduledExecutor();
2、ScheduledExecutorService pools = Executors.newScheduledThreadPool(3);
其它创建查看API
三、Demo
package com.yezi.learn.exectors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Created by yezi on 2014/5/10.
*/
public class ThreadPools {
public static void main(String []args){
//testPools();
//testScheduledPools();
testSingleScheduled();
}
public static void testSingleScheduled(){
ScheduledExecutorService pools = Executors.newSingleThreadScheduledExecutor();
pools.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("bombing");
}
},10,2,TimeUnit.SECONDS); //10秒后执行线程,每隔2秒执行一次
}
public static void testScheduledPools(){
ScheduledExecutorService pools = Executors.newScheduledThreadPool(3); //创建容量为3的线程池
for(int i=0;i<10;i++){
final int task = i;
pools.schedule(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 3; j++) {
try {
Thread.sleep(20);
System.out.println(Thread.currentThread().getName() + "====" + task + "====" + j);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, 2,TimeUnit.SECONDS); //2秒后开始执行线程
}
}
public static void testPools(){
//ExecutorService pools = Executors.newFixedThreadPool(3);
ExecutorService pools = Executors.newSingleThreadExecutor();
//ExecutorService pools = Executors.newCachedThreadPool();
for(int i=0;i<10;i++){
final int task = i;
pools.execute(new Runnable() {
@Override
public void run() {
for(int j=0;j<3;j++){
try {
Thread.sleep(20);
System.out.println(Thread.currentThread().getName()+"===="+task+"===="+j);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
pools.shutdown();
}
}
1、ExecutorService pools = Executors.newCachedThreadPool();
2、ExecutorService pools = Executors.newSingleThreadExecutor();
3、ExecutorService pools = Executors.newFixedThreadPool(3);
每种的不同点看JDK的API
二、使用Executors来创建Scheduled线程池
1、ScheduledExecutorService pools = Executors.newSingleThreadScheduledExecutor();
2、ScheduledExecutorService pools = Executors.newScheduledThreadPool(3);
其它创建查看API
三、Demo
package com.yezi.learn.exectors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Created by yezi on 2014/5/10.
*/
public class ThreadPools {
public static void main(String []args){
//testPools();
//testScheduledPools();
testSingleScheduled();
}
public static void testSingleScheduled(){
ScheduledExecutorService pools = Executors.newSingleThreadScheduledExecutor();
pools.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("bombing");
}
},10,2,TimeUnit.SECONDS); //10秒后执行线程,每隔2秒执行一次
}
public static void testScheduledPools(){
ScheduledExecutorService pools = Executors.newScheduledThreadPool(3); //创建容量为3的线程池
for(int i=0;i<10;i++){
final int task = i;
pools.schedule(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 3; j++) {
try {
Thread.sleep(20);
System.out.println(Thread.currentThread().getName() + "====" + task + "====" + j);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, 2,TimeUnit.SECONDS); //2秒后开始执行线程
}
}
public static void testPools(){
//ExecutorService pools = Executors.newFixedThreadPool(3);
ExecutorService pools = Executors.newSingleThreadExecutor();
//ExecutorService pools = Executors.newCachedThreadPool();
for(int i=0;i<10;i++){
final int task = i;
pools.execute(new Runnable() {
@Override
public void run() {
for(int j=0;j<3;j++){
try {
Thread.sleep(20);
System.out.println(Thread.currentThread().getName()+"===="+task+"===="+j);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
pools.shutdown();
}
}
相关文章推荐
- ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
- 线程池ThreadPoolExecutor使用简介
- Java四种线程池的使用
- 使用有界队列的线程池使用DiscardOldestPolicy拒绝策略
- Java-线程池专题(什么是线程池,如何使用,为什么要用)
- Java四种线程池的使用
- 深入浅出多线程(5)以并行包线程池为例说说线程池的设计需求及使用
- Java四种线程池的使用
- java并发编程(十六)----(线程池)java线程池的使用
- 使用线程池导出Excel数据
- Java并发编程:线程池的使用
- 线程池的使用
- 【Java先进】Lock、通过使用线程池
- JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用简介
- 线程池详解三 四种线程池使用实例
- java线程池介绍及简单使用举例
- java线程池使用及设计
- Java并发编程——线程池的使用
- 通过线程池使用多线程并发:ThreadPoolTaskExecutor 的应用例子
- Java 线程池的使用