线程池类:ExecutorServise
构造方法:
// 1
ExecutorService threadpool = Executors.newFixedThreadPool(n); //创建n个线程的线程池
// 2
ExecutorService threadpool = ExecutorsnewCacheThreadPool(); // execute()多少次就创建多少线程
具体操作:
ExecutorService threadpool = Executors.newFixedThreadPool(3);
for(int i = 0; i < 3; i++)
{
threadpool.execute( new Runnable() {
public void run()
{
...
}
});
}
/*
*Future Callable可以得到线程的返回值
*/
ExecutorService threadpool = Executors.newSingleThreadExecutor(); //注意构造方法
Future<String> future = threadpool.submit(new Callable<String>() {
public String call()
{
...
}
});