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核数

posted @ 2018-11-26 00:43  知识追求者  阅读(187)  评论(0编辑  收藏  举报