คลาส ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor เป็น ExecutorService เพื่อดำเนินการแต่ละงานที่ส่งโดยใช้เธรดที่รวมกันหลายเธรดซึ่งปกติจะกำหนดค่าโดยใช้เมธอด Executors factory นอกจากนี้ยังมีวิธีการยูทิลิตี้ต่างๆเพื่อตรวจสอบสถิติเธรดปัจจุบันและควบคุม
วิธี ThreadPoolExecutor
| ซีเนียร์ | วิธีการและคำอธิบาย |
|---|---|
| 1 | protected void afterExecute(Runnable r, Throwable t) วิธีที่เรียกใช้เมื่อเสร็จสิ้นการดำเนินการ Runnable ที่กำหนด |
| 2 | void allowCoreThreadTimeOut(boolean value) ตั้งค่านโยบายที่ควบคุมว่าเธรดหลักอาจหมดเวลาและสิ้นสุดลงหากไม่มีงานใด ๆ มาถึงภายในช่วงเวลาที่มีชีวิตอยู่โดยจะถูกแทนที่หากจำเป็นเมื่อมีงานใหม่มาถึง |
| 3 | boolean allowsCoreThreadTimeOut() ส่งคืนค่าจริงหากพูลนี้อนุญาตให้เธรดคอร์หมดเวลาและยุติหากไม่มีงานใด ๆ มาถึงภายในเวลา KeepAlive ซึ่งจะถูกแทนที่หากจำเป็นเมื่อมีงานใหม่มาถึง |
| 4 | boolean awaitTermination(long timeout, TimeUnit unit) บล็อกจนกว่างานทั้งหมดจะเสร็จสิ้นการดำเนินการหลังจากการร้องขอการปิดระบบหรือการหมดเวลาเกิดขึ้นหรือเธรดปัจจุบันถูกขัดจังหวะขึ้นอยู่กับว่าสิ่งใดจะเกิดขึ้นก่อน |
| 5 | protected void beforeExecute(Thread t, Runnable r) วิธีที่เรียกใช้ก่อนที่จะดำเนินการ Runnable ที่กำหนดในเธรดที่กำหนด |
| 6 | void execute(Runnable command) ดำเนินการงานที่กำหนดในอนาคต |
| 7 | protected void finalize() เรียกใช้การปิดระบบเมื่อตัวดำเนินการนี้ไม่ได้อ้างอิงอีกต่อไปและไม่มีเธรด |
| 8 | int getActiveCount() ส่งคืนจำนวนเธรดโดยประมาณที่กำลังรันงานอยู่ |
| 9 | long getCompletedTaskCount() ส่งคืนจำนวนงานทั้งหมดโดยประมาณที่ดำเนินการเสร็จสิ้น |
| 10 | int getCorePoolSize() ส่งคืนจำนวนแกนของเธรด |
| 11 | long getKeepAliveTime(TimeUnit unit) ส่งกลับค่าเวลาคงอยู่ของเธรดซึ่งเป็นระยะเวลาที่เธรดที่เกินขนาดพูลแกนหลักอาจไม่ได้ใช้งานก่อนที่จะถูกยกเลิก |
| 12 | int getLargestPoolSize() ส่งคืนจำนวนเธรดที่มากที่สุดที่เคยอยู่ในพูลพร้อมกัน |
| 13 | int getMaximumPoolSize() ส่งคืนจำนวนเธรดสูงสุดที่อนุญาต |
| 14 | int getPoolSize() ส่งคืนจำนวนเธรดปัจจุบันในพูล |
| 15 | BlockingQueue
ส่งคืนคิวงานที่ใช้โดยตัวดำเนินการนี้ |
| 15 | RejectedExecutionHandler getRejectedExecutionHandler() ส่งคืนตัวจัดการปัจจุบันสำหรับงานที่ไม่สามารถดำเนินการได้ |
| 16 | long getTaskCount() ส่งคืนจำนวนงานทั้งหมดโดยประมาณที่เคยกำหนดไว้สำหรับการดำเนินการ |
| 17 | ThreadFactory getThreadFactory() ส่งคืนโรงงานเธรดที่ใช้ในการสร้างเธรดใหม่ |
| 18 | boolean isShutdown() ส่งคืนจริงหากตัวดำเนินการนี้ถูกปิด |
| 19 | boolean isTerminated() ส่งคืนค่าจริงหากงานทั้งหมดเสร็จสิ้นหลังจากปิดเครื่อง |
| 20 | boolean isTerminating() ส่งคืนค่าจริงหากตัวดำเนินการนี้อยู่ในกระบวนการยุติการทำงานหลังจากปิดระบบ () หรือ shutdownNow () แต่ยังไม่สิ้นสุดอย่างสมบูรณ์ |
| 21 | int prestartAllCoreThreads() เริ่มต้นเธรดหลักทั้งหมดทำให้ไม่ต้องรอการทำงาน |
| 22 | boolean prestartCoreThread() เริ่มเธรดหลักทำให้รอการทำงาน |
| 23 | void purge() พยายามลบออกจากคิวงานงานในอนาคตทั้งหมดที่ถูกยกเลิก |
| 24 | boolean remove(Runnable task) ลบงานนี้ออกจากคิวภายในของตัวดำเนินการหากมีอยู่ดังนั้นจึงทำให้งานนี้ไม่ถูกรันหากยังไม่ได้เริ่มทำงาน |
| 25 | void setCorePoolSize(int corePoolSize) ตั้งค่าจำนวนแกนของเธรด |
| 26 | void setKeepAliveTime(long time, TimeUnit unit) ตั้งค่าการ จำกัด เวลาที่เธรดอาจไม่ได้ใช้งานก่อนที่จะยุติ |
| 27 | void setMaximumPoolSize(int maximumPoolSize) ตั้งค่าจำนวนเธรดสูงสุดที่อนุญาต |
| 28 | void setRejectedExecutionHandler(RejectedExecutionHandler handler) ตั้งค่าตัวจัดการใหม่สำหรับงานที่ไม่สามารถดำเนินการได้ |
| 29 | void setThreadFactory(ThreadFactory threadFactory) ตั้งค่าโรงงานเธรดที่ใช้สร้างเธรดใหม่ |
| 30 | void shutdown() เริ่มต้นการปิดระบบอย่างเป็นระเบียบซึ่งมีการดำเนินงานที่ส่งไปก่อนหน้านี้ แต่จะไม่มีการยอมรับงานใหม่ |
| 31 | List<Runnable> shutdownNow() พยายามหยุดงานที่กำลังดำเนินการอยู่ทั้งหมดหยุดการประมวลผลของงานที่รอและส่งคืนรายการงานที่รอการดำเนินการ |
| 32 | protected void terminated() เรียกเมธอดเมื่อผู้ดำเนินการยกเลิก |
| 33 | String toString() ส่งคืนสตริงที่ระบุพูลนี้ตลอดจนสถานะรวมถึงการบ่งชี้สถานะการรันและจำนวนผู้ปฏิบัติงานและงานโดยประมาณ |
ตัวอย่าง
โปรแกรม TestThread ต่อไปนี้แสดงการใช้อินเตอร์เฟส ThreadPoolExecutor ในสภาพแวดล้อมแบบเธรด
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestThread {
public static void main(final String[] arguments) throws InterruptedException {
ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool();
//Stats before tasks execution
System.out.println("Largest executions: "
+ executor.getLargestPoolSize());
System.out.println("Maximum allowed threads: "
+ executor.getMaximumPoolSize());
System.out.println("Current threads in pool: "
+ executor.getPoolSize());
System.out.println("Currently executing threads: "
+ executor.getActiveCount());
System.out.println("Total number of threads(ever scheduled): "
+ executor.getTaskCount());
executor.submit(new Task());
executor.submit(new Task());
//Stats after tasks execution
System.out.println("Core threads: " + executor.getCorePoolSize());
System.out.println("Largest executions: "
+ executor.getLargestPoolSize());
System.out.println("Maximum allowed threads: "
+ executor.getMaximumPoolSize());
System.out.println("Current threads in pool: "
+ executor.getPoolSize());
System.out.println("Currently executing threads: "
+ executor.getActiveCount());
System.out.println("Total number of threads(ever scheduled): "
+ executor.getTaskCount());
executor.shutdown();
}
static class Task implements Runnable {
public void run() {
try {
Long duration = (long) (Math.random() * 5);
System.out.println("Running Task! Thread Name: " +
Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(duration);
System.out.println("Task Completed! Thread Name: " +
Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
สิ่งนี้จะให้ผลลัพธ์ดังต่อไปนี้
เอาต์พุต
Largest executions: 0
Maximum allowed threads: 2147483647
Current threads in pool: 0
Currently executing threads: 0
Total number of threads(ever scheduled): 0
Core threads: 0
Largest executions: 2
Maximum allowed threads: 2147483647
Current threads in pool: 2
Currently executing threads: 2
Total number of threads(ever scheduled): 2
Running Task! Thread Name: pool-1-thread-2
Running Task! Thread Name: pool-1-thread-1
Task Completed! Thread Name: pool-1-thread-1
Task Completed! Thread Name: pool-1-thread-2