Java中线程池的使用

1.缓存线程池(长度无限制)

执行流程:判断线程池是否存在空闲线程

                  存在则使用

                  不存在,则创建线程,并放入线程池

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

public class ThreadPool {
    //缓存线程池
    public static void main(String[] args) {
        ExecutorService ex = Executors.newCachedThreadPool();
        ex.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"锄禾日当日");
            }
        });
        ex.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"锄禾日当日");
            }
        });
        ex.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"锄禾日当日");
            }
        });
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ex.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"锄禾日当日");
            }
        });
    }
}

 执行结果:

Java中线程池的使用

 

 2.定长线程池(长度是指定的数值)

  执行流程:1.判断线程池是否存在空闲线程

                    2.存在则使用

                    3.不存在空闲线程,且线程池未满的,则创建线程并放入线程池,然后使用

                    4.不存在空闲线程,且线程池已满的情况下,则等待线程池存在空闲线程

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

public class ThreadPoolFix {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"窗前民航业");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"窗前民航业");
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"hehheheh");
            }
        });
    }
}

  执行结果

Java中线程池的使用

 

 3.单线程池

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

public class SingleThreadPool {
    public static void main(String[] args) {
        ExecutorService ex = Executors.newSingleThreadExecutor();
        ex.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"hehehehe");
            }
        });
        ex.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"wwwwww");
            }
        });
        ex.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"hhhhhh");
            }
        });

    }
}

 执行结果

Java中线程池的使用

 

 4.周期性定长线程池

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

public class ThreadPoolSecd {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

//        scheduledExecutorService.schedule(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println(Thread.currentThread().getName()+"原来如此");
//            }
//        },5, TimeUnit.SECONDS);
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
                                                         @Override
                                                         public void run() {
                                                             System.out.println(Thread.currentThread().getName()+"旱地和西游");
                                                         }
                                                     },5,2,TimeUnit.SECONDS
        );
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"旱地和红楼");
            }
            },5,2,TimeUnit.SECONDS
        );
    }
}

  Java中线程池的使用

 

 

 

 

 

Java中线程池的使用

上一篇:面试十五、排序


下一篇:Unity 一键自动截图切换截多个屏幕尺寸