线程池
JAVA类库提供了许多静态方法来创建一个线程池。github https://github.com/Jacwo/one.git
- public static ExecutorService newCachedThreadPool();
- public static ExecutorService newSingleThreadExecutor();
- public static ExecutorService newFixedThreadPool(int paramInt);
- public static ScheduledExecutorService newScheduledThreadPool(int paramInt);
package com.falshhold.main;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ThreadDemo {
public static void main(String[] args) {
// 线程个数固定
//ExecutorService threadPool = Executors.newFixedThreadPool(5);
//一个线程执行操作
//ExecutorService threadPool = Executors.newSingleThreadExecutor();
// List<Future<String>> resultList = new ArrayList<Future<String>>();
// 线程定时完成操作
// ScheduledThreadPoolExecutor threadPool = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(10);
ExecutorService threadPool = Executors.newCachedThreadPool();
for(int i = 0; i < 10; i++) {
Future<String> future=threadPool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
//int cpuNums = Runtime.getRuntime().availableProcessors();
//System.out.println(cpuNums);
for (int i = 999999; i > 0; i--) ;
System.out.println("call()方法被自动调用,干活!!! " + Thread.currentThread().getName());
//Thread.sleep(1000);
return "call()方法被自动调用,任务的结果是: " + Thread.currentThread().getName();
}
});
resultList.add(future);
}
threadPool.shutdown(); //执行到此处并不会马上关闭线程池,但之后不能再往线程池中加线程,否则会报错
System.out.println("Main: Finished all threads at"+ new Date());
for (Future<String> fs : resultList) {
try {
System.out.println(fs.get()); //打印各个线程(任务)执行的结果
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
}
}
/* threadPool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
//int cpuNums = Runtime.getRuntime().availableProcessors();
//System.out.println(cpuNums);
System.out.println(Thread.currentThread().getName()+new Date());
Thread.sleep(1000);
return "hahahahha";
}
});*/
}
}
submit()和excute()方法的区别,
submit执行带返回值,excute没有返回值。
线程池详解
Executors.newCacheThreadPool()
创建一个可缓存的线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则创建新的线程
Executors cacheThreadPool=Executors.newCacheThreadPool();
for(int i=0;i<100;i++){
cacheThreadPool.execute(new Runnable(){
@Override
public void run(){
system.out.println(index);
}
});
}
}
线程1执行完毕,线程2会复用线程1的线程
------------------------------------------------------------------------------------------------
Executors.newFixedThreadPool()
创建一个定长的线程,超过数量将在队列中等待
Executors cacheThreadPool=Executors.newFixedThreadPool();
for(int i=0;i<100;i++){
final int i=index;
cacheThreadPool.execute(new Runnable(){
@Override
public void run(){
system.out.println(i);
}
});
}
}
定长线程池的大小最好根据系统资源进行设置。如Runtime.getRuntime().availableProcessors()
------------------------------------------------------------------------------------------------
Executors.newScheduledThreadPool(5);
创建一个定长线程池,支持定时以及周期性任务执行
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
scheduledThreadPool.schedule(new Runnable() {
@Override
public void run() {
System.out.println("delay 3 seconds");
}
}, 3, TimeUnit.SECONDS);
------------------------------------------------------------------------------------------------
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
创建一个单线程化线程池,只会使用唯一的线程执行任务,保证所有任务按顺序执行
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println(index);
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}