1. 程式人生 > >java多執行緒快速入門(二十二)

java多執行緒快速入門(二十二)

執行緒池的好處:

  避免我們過多的去new執行緒,new是佔資源的(GC主要堆記憶體)

  提高效率

  避免浪費資源

  提高響應速度

作用:會把之前執行某個執行緒完畢的執行緒不會釋放掉會留到執行緒池中給下一個呼叫的執行緒直接使用

前提:AB執行緒在同一個執行緒池裡面;A執行緒執行完畢了,B執行緒進來了,就直接去替換原來A執行緒的run方法,執行B

使用newCachedThreadPool建立執行緒池

package com.cppdy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread20 implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()+"-執行緒執行開始"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()
+"-執行緒執行結束"); } } public class ThreadDemo20 { public static void main(String[] args) throws Exception { //建立一個可快取執行緒池,如果執行緒池長度超過處理需要,可靈活回收空閒執行緒,如無可回收,則新建執行緒 ExecutorService newCachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { Thread.sleep(
500); newCachedThreadPool.execute(new Thread(new MyThread20(),"執行緒1")); } //釋放執行緒池 newCachedThreadPool.shutdown(); } }
View Code

使用newFixedThreadPool建立執行緒池

package com.cppdy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread21 implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "-執行緒執行開始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-執行緒執行結束");
    }
}

public class ThreadDemo21 {

    public static void main(String[] args) throws Exception {
        // 建立一個定長執行緒池,可控制執行緒最大併發數,超出的執行緒會在佇列中等待
        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Thread.sleep(500);
            newFixedThreadPool.execute(new Thread(new MyThread21(), "執行緒1"));
        }
        // 釋放執行緒池
        newFixedThreadPool.shutdown();
    }

}
View Code

使用newScheduledThreadPool建立執行緒池

package com.cppdy;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadDemo22 {

    public static void main(String[] args) throws Exception {
        // 建立一個定長執行緒池,支援定時及週期性任務執行
        ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(5);
        for (int i = 0; i < 10; i++) {
            final int temp = i;
            newScheduledThreadPool.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println("i:" + temp);
                }
            }, 3, TimeUnit.SECONDS);
        }
        // 釋放執行緒池
        newScheduledThreadPool.shutdown();
    }

}
View Code

使用newSingleThreadExecutor建立執行緒池

package com.cppdy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread23 implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "-執行緒執行開始");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-執行緒執行結束");
    }
}

public class ThreadDemo23 {

    public static void main(String[] args) throws Exception {
        // 建立一個單執行緒的執行緒池,它只會用唯一的工作執行緒來執行任務
        ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            Thread.sleep(500);
            newSingleThreadExecutor.execute(new Thread(new MyThread21(), "執行緒1"));
        }
        // 釋放執行緒池
        newSingleThreadExecutor.shutdown();
    }

}
View Code

CPU密集型時,任務可以少配置執行緒數,大概和機器的cpu核數相當,這樣可以使得每個執行緒都在執行任務

IO密集型時,大部分執行緒都阻塞,故需要多配置執行緒數,2*cpu核數