Java 线程池创建 理解(简单)(只要反复理解,就能理解)

线程池创建简单理解

如果还没有学习Runnable多线程,请先看我另外一篇Runnable的文章

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

public class cachethreadpool {
    public static void main (String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(3);
        int setup = 1;
        for (int i = 0; i < setup; i++) { //for循环开始
            final int index = i;
            setup++;
            es.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(index);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        } //for循环结束
    }
}

(个人理解)步骤:

  1. //新建ExecutorService(线程池)

ExecutorService es = new Executors.newFixedThreadPool(3); //线程池大小为3,所以创建三个线程

  1. //用ExecutorService的execute方法执行线程池中的线程

es.execute(new Runnable() { //新建Runnable
    @Override
    public void run() { //重写run方法(Runnable)
            try {
               System.out.println(index);
               Thread.sleep(1000);
         } catch (InterruptedException e) {
                e.printStackTrace();
           }
      }
}); //好像还挺简单的…

 

深入理解

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;



public class cachethreadpool {

    public static void main (String[] args) {

        ExecutorService es = Executors.newFixedThreadPool(3);

        Prints pts = new Prints(1);

        while (true) {

            es.execute(pts);

        }

        /*Prints pts = new Prints(1);

        pts.run();*/

    }

}



class Prints implements Runnable {

    int thnum;

    public Prints(int thnum) {

        this.thnum = thnum;

    }

    public void run() {

        Long time;

        Long time2;

        Long time3;

        time = System.currentTimeMillis();

        for (int i = 0; i < 20; i++) {

            try {

                System.out.println("线程" + thnum + " NO." + i);

                Thread.sleep(1000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

        time2 = System.currentTimeMillis();

        time3 = time2 - time;

        System.out.println(time3);

    }

}

注意:给es.execute加了一个for循环,实际上在执行

ExecutorService es = Executors.newFixedThreadPool(3);

的时候,es就已经开始多线程线程了,而execute实际上可以指定执行很多个Runnable任务(class),只有一个限制,就是一个周期只能运行3个。

修改一下代码,再次运行尝试:

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;



public class cachethreadpool {

    public static void main (String[] args) {

        ExecutorService es = Executors.newFixedThreadPool(3);

        Prints pts = new Prints(1);

        Prints2 pts2 = new Prints2(2);

        while (true) {

            es.execute(pts);

            es.execute(pts2);

        }

        /*Prints pts = new Prints(1);

        pts.run();*/

    }

}



class Prints implements Runnable {

    int thnum;

    public Prints(int thnum) {

        this.thnum = thnum;

    }

    public void run() {

        Long time;

        Long time2;

        Long time3;

        time = System.currentTimeMillis();

        for (int i = 0; i < 20; i++) {

            try {

                System.out.println("线程" + thnum + " NO." + i);

                Thread.sleep(1000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

        time2 = System.currentTimeMillis();

        time3 = time2 - time;

        System.out.println(time3);

    }

}



class Prints2 implements Runnable {

    int thnum;

    public Prints2(int thnum) {

        this.thnum = thnum;

    }

    public void run() {

        Long time;

        Long time2;

        Long time3;

        time = System.currentTimeMillis();

        for (int i = 0; i < 20; i++) {

            try {

                System.out.println("线程" + thnum + " NO." + i);

                Thread.sleep(1000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

        time2 = System.currentTimeMillis();

        time3 = time2 - time;

        System.out.println(time3);

    }

}

运行结果:

线程2 NO.0

线程1 NO.0

线程1 NO.0

线程1 NO.1

线程1 NO.1

线程2 NO.1

线程1 NO.2

线程2 NO.2

线程1 NO.2

线程1 NO.3

线程1 NO.3

线程2 NO.3

注意:运行结果每1秒会出现3个(因为有个sleep),而按照什么顺序执行实际上是看pts和pts2两个线程哪个先抢到cpu的资源。

其它线程池…

Java通过Executors提供四种线程池,分别为:
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。 弹性线程池,无上限线程数(但要小心因为线程太多而导致的崩溃)
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。 推荐,一次只能运行特定数量的线程
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值