编程语言
首页 > 编程语言> > Java中线程池的使用

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()+"锄禾日当日");
            }
        });
    }
}

 执行结果:

 

 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");
            }
        });
    }
}

  执行结果

 

 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");
            }
        });

    }
}

 执行结果

 

 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,Thread,void,Runnable,线程,new,中线,public
来源: https://www.cnblogs.com/wanshiliang/p/15207701.html