多线程编程6-----Executors的使用
2014-09-01 16:00
120 查看
package zhu.ThreadPools;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Demo1 {
public static void main(String[] args) {
//huoqu 线程池 且指定线程
ExecutorService threadPool0= Executors.newFixedThreadPool(3);//固定3个线程
// ExecutorService threadPool1= Executors.newCachedThreadPool();//自动增减线程
ExecutorService threadPool2= Executors.newSingleThreadExecutor();//线程池只有一个线程 单线程 可以保证单线程死了之后重新建立单个线程
for(int i=1;i<11;i++){
final int task = i;
//每次只有三个线程交替执行
threadPool0.execute(
new Runnable()
{
public void run()
{
for(int i=1;i<11;i++)
{
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"is loop"+i+","+task);}
}
});
}
System.out.println("10 task have commited");
// threadPool.shutdownNow();//立即关闭线程池
//定时线程执行
Executors.newScheduledThreadPool(3)
.schedule
(new Runnable() {
@Override
public void run() {
System.out.println("boom!!!!");
}
}, 4, TimeUnit.SECONDS);
//定时线程执行2 固定的平率
Executors.newScheduledThreadPool(3)
.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("bb");
}
}, 6, //多少时间开始 相对时间, 解决定时 开始时间-当前时间
2, //每隔多少秒执行一次
TimeUnit.SECONDS);
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Demo1 {
public static void main(String[] args) {
//huoqu 线程池 且指定线程
ExecutorService threadPool0= Executors.newFixedThreadPool(3);//固定3个线程
// ExecutorService threadPool1= Executors.newCachedThreadPool();//自动增减线程
ExecutorService threadPool2= Executors.newSingleThreadExecutor();//线程池只有一个线程 单线程 可以保证单线程死了之后重新建立单个线程
for(int i=1;i<11;i++){
final int task = i;
//每次只有三个线程交替执行
threadPool0.execute(
new Runnable()
{
public void run()
{
for(int i=1;i<11;i++)
{
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"is loop"+i+","+task);}
}
});
}
System.out.println("10 task have commited");
// threadPool.shutdownNow();//立即关闭线程池
//定时线程执行
Executors.newScheduledThreadPool(3)
.schedule
(new Runnable() {
@Override
public void run() {
System.out.println("boom!!!!");
}
}, 4, TimeUnit.SECONDS);
//定时线程执行2 固定的平率
Executors.newScheduledThreadPool(3)
.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("bb");
}
}, 6, //多少时间开始 相对时间, 解决定时 开始时间-当前时间
2, //每隔多少秒执行一次
TimeUnit.SECONDS);
}
}
相关文章推荐
- 什么时候该使用多线程,以及更恰当的多线程编程方法之讨论
- 多线程编程之使用工作线程实现文档自动保存(II)
- 多线程编程学习3——使用MFC工作者线程
- C#中使用多线程编程之线程池
- Java 多线程编程之三:synchronized 关键字的使用
- python 进行多线程编程,使用Semaphore控制线程数
- 多线程编程之重点--使用DSP/BIOS时选择线程类型的参考方法
- 灵活使用多线程编程——EventWaitHandle类
- Solaris2.4 多线程编程指南3--使用同步对象编程
- C# 多线程编程 - 使用委托创建线程
- 使用pthread库进行多线程编程1 - UNIX环境高级编程第11章读书笔记
- 使用pthread库进行多线程编程1 - UNIX环境高级编程第11章读书笔记
- 使用pthread库进行多线程编程2 - UNIX高级环境编程第12章读书笔记
- 多线程编程:何时使用同步类
- 如何使用Java编写多线程程序-Java基础-Java-编程开发
- 多线程编程之使用工作线程实现文档自动保存(I)
- 使用pthread库进行多线程编程2 - UNIX高级环境编程第12章读书笔记
- (转帖)使用多线程编程的几个常见问题- -
- Java6学习笔记60——多线程编程——使用volatile保障原子性
- C# 多线程编程 - 使用Thread类创建线程