文本浏览器

Java线程池创建简单理解

发布者 : 管理员-Adler | 发布时间 : 2019-01-17 19:16:36
文章号 : 22 | 阅读量 : 7+1 | AAW值(?) : 0.00 (仅供参考)

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 = 0i < setupi++) { //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,所以创建三个线程

2.    //ExecutorServiceexecute方法执行线程池中的线程

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),而按照什么顺序执行实际上是看ptspts2两个线程哪个先抢到cpu的资源。

其它线程池

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






评论加载中...

+ 参与讨论