Executors 類的 newCachedThreadPool() 方法創建了一個線程池,該池根據需要創建新線程,但會在可用時重用先前構造的線程。
用法
public static ExecutorService newCachedThreadPool()
public static ExecutorService newCachedThreadPool (ThreadFactory threadFactory)
參數
threadFactory - 創建新線程時使用的工廠
返回
新創建的線程池
拋出
NullPointerException
例子1
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ExecutornewCachedThreadPoolExample1 {
public static void main(final String[] arguments) throws InterruptedException {
ExecutorService excr = Executors.newCachedThreadPool();
ThreadPoolExecutor mypool = (ThreadPoolExecutor) excr;
//print statement will print the pool size.
System.out.println("size of mypool:" + mypool.getPoolSize());
excr.submit(new Threadimpl());
excr.submit(new Threadimpl());
System.out.println("Total number threads scheduled):"+ mypool.getTaskCount());
excr.shutdown();
}
static class Threadimpl implements Runnable {
public void run() {
try {
Long num = (long) (Math.random() / 30);
System.out.println("Thread Name:" +Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(num);
System.out.println("after sleep Thread Name:" +Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
輸出:
size of mypool:0 Total number threads scheduled):2 Thread Name:pool-1-thread-2 Thread Name:pool-1-thread-1 after sleep Thread Name:pool-1-thread-2 after sleep Thread Name:pool-1-thread-1
例子2
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ExecutornewCachedThreadPoolExample2 {
public static void main(final String[] arguments) throws InterruptedException {
ThreadFactory ThreadFactory = Executors.defaultThreadFactory();
ExecutorService excr = Executors.newCachedThreadPool(ThreadFactory);
ThreadPoolExecutor mypool = (ThreadPoolExecutor) excr;
System.out.println("size of mypool:" + mypool.getPoolSize());
for(int i=0 ; i<= 4 ; i++) {
excr.submit(new Threadimpl());
}
excr.shutdown();
}
static class Threadimpl implements Runnable {
public void run() {
try {
Long num = (long) (Math.random() / 30);
System.out.println("Thread Name:" +Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(num);
System.out.println("after sleep Thread Name:" +Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
輸出:
Thread Name:pool-1-thread-4 after sleep Thread Name:pool-1-thread-4 Thread Name:pool-1-thread-3 after sleep Thread Name:pool-1-thread-3 Thread Name:pool-1-thread-1 after sleep Thread Name:pool-1-thread-1
相關用法
- Java Executors newScheduledThreadPool()用法及代碼示例
- Java Executors newSingleThreadExecutor()用法及代碼示例
- Java Executors newFixedThreadPool()用法及代碼示例
- Java Executors Callable()用法及代碼示例
- Java Executors defaultThreadFactory()用法及代碼示例
- Java Executor newWorkStealingPool()用法及代碼示例
- Java Executor privilegedThreadFactory()用法及代碼示例
- Java Enum equals()用法及代碼示例
- Java EnumSet range()用法及代碼示例
- Java EnumMap remove()用法及代碼示例
- Java Enum toString()用法及代碼示例
- Java EnumMap hashCode()用法及代碼示例
- Java EnumMap get()用法及代碼示例
- Java EnumSet complementOf()用法及代碼示例
- Java Enum ordinal()用法及代碼示例
- Java EnumSet clone()用法及代碼示例
- Java EnumMap entrySet()用法及代碼示例
- Java EnumMap containsKey()用法及代碼示例
- Java EnumMap containsValue(value)用法及代碼示例
- Java Enumeration asIterator()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Executors newCachedThreadPool() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。