package Demo9;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
/**
* @param args
*/
public static void main(String[] args) {
// ExecutorService threadPool = Executors.newFixedThreadPool(3);
ExecutorService threadPool = Executors.newCachedThreadPool();
//创建单一线程池(线程死了如何重新启动??)
//ExecutorService threadPool = Executors.newSingleThreadExecutor();
//Executors.n
for(int j=0;j<=10;j++){
final int task=j;
threadPool.execute(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=1;i<=10;i++){
System.out.println(Thread.currentThread().getName()+"is loop of"+i+"task of"+task);
}
}
});
}
System.out.println("all of 10 tasks have commits");
// threadPool.shutdown();
Executors.newScheduledThreadPool(3).schedule(new Runnable(){
@Override
public void run() {
System.out.println("bombing!!");
}
}, 10,TimeUnit.SECONDS);
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
System.out.println("bombing!!");
}
}, 6, 2, TimeUnit.SECONDS);
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
/**
* @param args
*/
public static void main(String[] args) {
// ExecutorService threadPool = Executors.newFixedThreadPool(3);
ExecutorService threadPool = Executors.newCachedThreadPool();
//创建单一线程池(线程死了如何重新启动??)
//ExecutorService threadPool = Executors.newSingleThreadExecutor();
//Executors.n
for(int j=0;j<=10;j++){
final int task=j;
threadPool.execute(new Runnable(){
@Override
public void run() {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=1;i<=10;i++){
System.out.println(Thread.currentThread().getName()+"is loop of"+i+"task of"+task);
}
}
});
}
System.out.println("all of 10 tasks have commits");
// threadPool.shutdown();
Executors.newScheduledThreadPool(3).schedule(new Runnable(){
@Override
public void run() {
System.out.println("bombing!!");
}
}, 10,TimeUnit.SECONDS);
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
System.out.println("bombing!!");
}
}, 6, 2, TimeUnit.SECONDS);
}
}