import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 九、线程与并发库的应用(线程池)
*
* @author dahai
*
*/
public class ThreadPoolTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//一、创建一个有三个线程的线程池(固定线程池)
//ExecutorService threadPool = Executors.newFixedThreadPool(3);
//二、创建缓存线程池(线程个数是动态的)
//ExecutorService threadPool = Executors.newCachedThreadPool();
//三、创建一个线程 (保证永远有一个线程,线程死来一个替补)
// ExecutorService threadPool = Executors.newSingleThreadExecutor();
// for (int i = 0; i < 10; i++) {
// int task = i;
// // 执行任务
// threadPool.execute(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// for (int i = 0; i < 10; i++) {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println(
// Thread.currentThread().getName() + " is looping of " + i + "!!! is task for : " + task);
// }
// }
// });
// }
// //线程结束
// threadPool.shutdown();
//用线程启动定时器
ScheduledExecutorService threadPool2 = Executors.newScheduledThreadPool(3);
threadPool2.schedule(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("begining star !!!");
}
}, 5,TimeUnit.SECONDS);
//七秒后执行,每隔2秒后继续执行
threadPool2.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("begining star !!!");
}
}, 7, 2, TimeUnit.SECONDS);
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 九、线程与并发库的应用(线程池)
*
* @author dahai
*
*/
public class ThreadPoolTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//一、创建一个有三个线程的线程池(固定线程池)
//ExecutorService threadPool = Executors.newFixedThreadPool(3);
//二、创建缓存线程池(线程个数是动态的)
//ExecutorService threadPool = Executors.newCachedThreadPool();
//三、创建一个线程 (保证永远有一个线程,线程死来一个替补)
// ExecutorService threadPool = Executors.newSingleThreadExecutor();
// for (int i = 0; i < 10; i++) {
// int task = i;
// // 执行任务
// threadPool.execute(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// for (int i = 0; i < 10; i++) {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println(
// Thread.currentThread().getName() + " is looping of " + i + "!!! is task for : " + task);
// }
// }
// });
// }
// //线程结束
// threadPool.shutdown();
//用线程启动定时器
ScheduledExecutorService threadPool2 = Executors.newScheduledThreadPool(3);
threadPool2.schedule(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("begining star !!!");
}
}, 5,TimeUnit.SECONDS);
//七秒后执行,每隔2秒后继续执行
threadPool2.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("begining star !!!");
}
}, 7, 2, TimeUnit.SECONDS);
}
}