4种Java线程池用法以及线程池的做用和优势,你都知道了没?

1、以下方式存在的问题程序员

new Thread() {
    @Override
    public void run() {
        // 业务逻辑
    }
}.start();

一、首先频繁的建立、销毁对象是一个很消耗性能的事情;数据库

二、若是用户量比较大,致使占用过多的资源,可能会致使咱们的服务因为资源不足而宕机;服务器

三、综上所述,在实际的开发中,这种操做实际上是不可取的一种方式。app

v2-3a18167ada4a86421dd99a6cc2ba5d80_hd.png

2、使用线程池有什么优势ide

一、线程池中线程的使用率提高,减小对象的建立、销毁;性能

二、线程池能够控制线程数,有效的提高服务器的使用资源,避免因为资源不足而发生宕机等问题;测试

v2-d2e490c18e85eda9992ccc760c93fb65_hd.png

3、线程池的四种使用方式spa

一、newCachedThreadPool线程

建立一个线程池,若是线程池中的线程数量过大,它能够有效的回收多余的线程,若是线程数不足,那么它能够建立新的线程。设计

public static void method() throws Exception {
    ExecutorService executor = Executors.newCachedThreadPool();
    for (int i = 0; i < 5; i++) {
        final int index = i;
        Thread.sleep(1000);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "  " + index);
            }
        });
    }
}

执行结果

v2-d0abbd7ae6a1172de46020e52397a13f_hd.jpg

经过分析我看能够看到,至始至终都由一个线程执行,实现了线程的复用,并无建立多余的线程。

若是当咱们的业务须要必定的时间进行处理,那么将会出现什么结果。咱们来模拟一下。

v2-e5177836df8fd79afd3a6dd27c898eea_hd.gif

能够明显的看出,如今就须要几条线程来交替执行。

不足:这种方式虽然能够根据业务场景自动的扩展线程数来处理咱们的业务,可是最多须要多少个线程同时处理缺是咱们没法控制的;

优势:若是当第二个任务开始,第一个任务已经执行结束,那么第二个任务会复用第一个任务建立的线程,并不会从新建立新的线程,提升了线程的复用率;

二、newFixedThreadPool

这种方式能够指定线程池中的线程数。举个栗子,若是一间澡堂子最大只能容纳20我的同时洗澡,那么后面来的人只能在外面排队等待。若是硬往里冲,那么只会出现一种情景,摩擦摩擦...

首先测试一下最大容量为一个线程,那么会不会是咱们预测的结果。

public static void method_01() throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(1);
    for (int i = 0; i < 10; i++) {
        Thread.sleep(1000);
        final int index = i;
        executor.execute(() -> {
            try {
                Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "  " + index);
        });
    }
    executor.shutdown();
}

执行结果

v2-132ec2dcc123c50542a1974f74e945f2_hd.gif

咱们改成3条线程再来看下结果

v2-c4197f28eeb7464e31e71bb009f53aa7_hd.gif

优势:两个结果综合说明,newFixedThreadPool的线程数是能够进行控制的,所以咱们能够经过控制最大线程来使咱们的服务器打到最大的使用率,同事又能够保证及时流量忽然增大也不会占用服务器过多的资源。

三、newScheduledThreadPool

该线程池支持定时,以及周期性的任务执行,咱们能够延迟任务的执行时间,也能够设置一个周期性的时间让任务重复执行。 该线程池中有如下两种延迟的方法。

  • scheduleAtFixedRate

测试一

public static void method_02() {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
    executor.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            long start = new Date().getTime();
            System.out.println("scheduleAtFixedRate 开始执行时间:" +
                    DateFormat.getTimeInstance().format(new Date()));
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long end = new Date().getTime();
            System.out.println("scheduleAtFixedRate 执行花费时间=" + (end - start) / 1000 + "m");
            System.out.println("scheduleAtFixedRate 执行完成时间:" + DateFormat.getTimeInstance().format(new Date()));
            System.out.println("======================================");
        }
    }, 1, 5, TimeUnit.SECONDS);
}

执行结果

v2-ea6c0472d1c437f98fabed6bf86cdbc8_hd.gif

测试二

v2-1b144cfb8b04f02d532c229073e8bc6c_hd.gif

总结:以上两种方式不一样的地方是任务的执行时间,若是间隔时间大于任务的执行时间,任务不受执行时间的影响。若是间隔时间小于任务的执行时间,那么任务执行结束以后,会立马执行,至此间隔时间就会被打乱。

  • scheduleWithFixedDelay

测试一

public static void method_03() {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
    executor.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            long start = new Date().getTime();
            System.out.println("scheduleWithFixedDelay 开始执行时间:" +
                    DateFormat.getTimeInstance().format(new Date()));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long end = new Date().getTime();
            System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "m");
            System.out.println("scheduleWithFixedDelay执行完成时间:"
                    + DateFormat.getTimeInstance().format(new Date()));
            System.out.println("======================================");
        }
    }, 1, 2, TimeUnit.SECONDS);
}

执行结果

v2-a8b74e9b7ee5b79ad91cd9b125c28aa7_hd.gif

测试二

public static void method_03() {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
    executor.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            long start = new Date().getTime();
            System.out.println("scheduleWithFixedDelay 开始执行时间:" +
                    DateFormat.getTimeInstance().format(new Date()));
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long end = new Date().getTime();
            System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "m");
            System.out.println("scheduleWithFixedDelay执行完成时间:"
                    + DateFormat.getTimeInstance().format(new Date()));
            System.out.println("======================================");
        }
    }, 1, 2, TimeUnit.SECONDS);
}

执行结果

v2-36017bd216804ae852f141626a414d84_hd.gif

总结:一样的,跟scheduleWithFixedDelay测试方法同样,能够测出scheduleWithFixedDelay的间隔时间不会受任务执行时间长短的影响。

四、newSingleThreadExecutor

这是一个单线程池,至始至终都由一个线程来执行。

public static void method_04() {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    for (int i = 0; i < 5; i++) {
        final int index = i;
        executor.execute(() -> {
            try {
                Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "   " + index);
        });
    }
    executor.shutdown();
}

执行结果

v2-4b5c9a11ac770c97b596878ff321856a_hd.gif

4、线程池的做用

线程池的做用主要是为了提高系统的性能以及使用率。文章刚开始就提到,若是咱们使用最简单的方式建立线程,若是用户量比较大,那么就会产生不少建立和销毁线程的动做,这会致使服务器在建立和销毁线程上消耗的性能可能要比处理实际业务花费的时间和性能更多。线程池就是为了解决这种这种问题而出现的。

v2-a92361353e887df13e533a753ad783d8_hd.png

欢迎你们关注个人公种浩【程序员追风】,文章都会在里面更新,整理的资料也会放在里面。

一样思想的设计还有不少,好比数据库链接池,因为频繁的链接数据库,然而建立链接是一个很消耗性能的事情,全部数据库链接池就出现了。

最后

欢迎你们一块儿交流,喜欢文章记得点个赞哟,感谢支持!

相关文章
相关标签/搜索