1 เหตุผลในการแนะนำพูลเธรด
เนื่องจากวงจรชีวิตของเธรดรวมถึงการสร้าง, พร้อม, วิ่ง, การบล็อกและทำลายขั้นตอนเมื่อจำนวนงานที่เรารอดำเนินการมีขนาดเล็กเราจึงสามารถสร้างเธรดหลายตัวเพื่อจัดการงานที่เกี่ยวข้อง แต่เมื่อมีจำนวนมาก ของงานการสร้างและการทำลายเธรดต้องใช้ค่าใช้จ่ายจำนวนมากและการใช้พูลเธรดจะช่วยบรรเทาปัญหาเหล่านี้ได้อย่างมาก
2 ใช้พูลเธรด
เราจำเป็นต้องใช้วิธีการคงที่ที่คลาสที่จัดทำโดยคลาส Executors เพื่อสร้างพูลเธรดที่เกี่ยวข้อง:
Public ExecutorSevice NewsingleTheTheDexecutor () Public Static ExecutorSevice NewFixedThreadPool () Public Static ExecutorSevice Newcachedthre Adpool ()
NewsingLetHreadExecutor ส่งคืนผู้ดำเนินการที่มีเธรดเดียว
NewFixedThreadPool ส่งคืนพูลเธรดที่มีจำนวนเธรดที่ระบุ
Newcachedthreadpool สร้างเธรดที่สอดคล้องกันตามจำนวนงานของผู้ใช้
เราเพียงแค่ต้องนำงานที่จะดำเนินการในวิธีการเรียกใช้และส่งผ่านคลาสการใช้งานของอินเตอร์เฟส runnable ไปยังวิธีการดำเนินการของพูลเธรดเป็นพารามิเตอร์ดังที่แสดงด้านล่าง:
Executor Executor = Executors.NewSingLetHreadExecutor (); Executor.Execute (ใหม่ runnable () {public void run () {// งานที่ดำเนินการ}}}}}หากคุณต้องการส่งผ่านพารามิเตอร์ไปยังงานคุณสามารถทำได้โดยการสร้างคลาสการใช้งานสำหรับอินเทอร์เฟซ Runnable
3. ตัวอย่าง
(1): NewsingLetHreadExecutor
Mythread.java
PublicClassMythread ขยายเธรด {@Override PublicVoid Run () {System.out.println (thread.currentthread (). getName () + "การดำเนินการ ... "); {// สร้างพูลเธรดที่สามารถนำมาใช้ซ้ำจำนวนของเธรด ExecutorService Pool = Executors Thread T2 = New Mythread (); Pool.execu TE (T2); ผลลัพธ์ผลลัพธ์
Pool-1-Thread-1 กำลังดำเนินการ - - Pool-1-Thread-1 กำลังดำเนินการ - - Pool-1-Thread-1 กำลังดำเนินการ - - Pool-1-Thread-1 กำลังดำเนินการ - - Pool-1-Thread-1 กำลังดำเนินการ - -
(2): newfixedthreadpool
testfixedthreadpool.java
PublicClass TestFixedThreadPool {PublicStaticVoid Main (String [] args) {// สร้างพูลเธรดที่สามารถใช้การคงที่จำนวนเธรด ExecutorService Pool = Executors.newFixedThreadPo OL (2); Interface thread t1 = new mythread (); สระว่ายน้ำ execute.execute (T1); }} ผลลัพธ์ผลลัพธ์
Pool-1-Thread-1 กำลังดำเนินการ - - Pool-1-Thread-2 กำลังดำเนินการ - - Pool-1-Thread-1 กำลังดำเนินการ - - Pool-1-Thread-2 กำลังดำเนินการ - - Pool-1-Thread-1 กำลังดำเนินการ - -
(3): newcachedthreadpool
testcachedthreadpool.java
PublicClass TestCachedThreadPool {PublicStaticVoid Main (String [] args) {// สร้างพูลเธรดที่สามารถนำกลับมาใช้ซ้ำจำนวนของเธรด ExecutorService Pool = Executors.newcachedThread Pool (); Interface thread t1 = new mythread (); ดำเนินการในพูล Execute (T1); } ผลลัพธ์ผลลัพธ์:
Pool-1-Thread-2 กำลังดำเนินการ - - Pool-1-Thread-4 กำลังดำเนินการ - - Pool-1-Thread-3 กำลังดำเนินการ - - Pool-1-Thread-1 กำลังดำเนินการ - - Pool-1-Thread-5 กำลังดำเนินการ - -
(4): NewscheduledThreadpool
TestscheduledThreadPoolexecutor.java
PublicClass TestsCheduledThreadPoolexecutor {PublicStaticVoid Main (String [] args) {ScheduledThreadPoolexecutor Exec = ใหม่ ScheduledThreadPoolexecut Tor (1); โยน runtimeException ใหม่ (); ====================================================== =========== Runna ble () {// พิมพ์เวลาระบบทุกครั้งในขณะที่พิสูจน์ว่าทั้งสองไม่มีอิทธิพลต่อกันและกัน @Override PublicVoid Run () {System.out.println (System.nanotime ()); ผลลัพธ์ผลลัพธ์
-