(线程四)线程池

线程池:顾名思义,充满线程的池子,通过调用Executors类方法来实现创建线程池以及进行任务的任务。

例一:通过Executors类静态方法ExecutorService newFixedThreadPool(int nThread)(混合线程)来创建线程池。再调用execute方法将任务添加至线程池后便可以运行。

                        import java.util.concurrent.ExecutorService;
                        import java.util.concurrent.Executors;
                        public class ThreadpoolDemo1 {
                            public static void main(String[] args) {
                                ExecutorService pool=Executors.newFixedThreadPool(2);
                                Run r1=new Run("r1");
                                Run r2=new Run("r2");
                                Run r3=new Run("r3");
                                Run r4=new Run("r4");
                                pool.execute(r1);
                                pool.execute(r2);
                                pool.execute(r3);
                                pool.execute(r4);
                            }
                        }
                        class Run implements Runnable{
                            private String name;
                            public Run(String name) {
                                this.name=name;
                            }
                            public void run() {
                                for(int i=0;i<3;i++) {
                                    System.out.println(name+":"+i);
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        运行结果:
                        r1:0
                        r2:0
                        r1:1
                        r2:1
                        r2:2
                        r1:2
                        r3:0
                        r4:0
                        r3:1
                        r4:1
                        r4:2
                        r3:2

                        有结果可知,该方法开始任务的顺序为添加任务顺序,nThread参数为可同时开始的线程数,但是任务结束后为什么程序仍在运行?因为我们指定的可开始的线程有两个,但是其中线程池中一共有多少个线程我们并不知道,所以可能还会有其他线程在进行,所以程序并没有结束

例二:通过Executors类静态方法ExecutorService newSingleThreadExecutor()(单个线程)来创建线程池。再调用execute方法将任务添加至线程池后便可以运行。

                        import java.util.concurrent.ExecutorService;
                        import java.util.concurrent.Executors;
                        public class ThreadpoolDemo2 {
                            public static void main(String[] args) {
                                ExecutorService pool= Executors.newSingleThreadExecutor();
                                Run r1=new Run("A");
                                Run r2=new Run("B");
                                Run r3=new Run("C");
                                pool.execute(r1);
                                pool.execute(r2);
                                pool.execute(r3);
                            }
                        }
                        class Run implements Runnable{
                            private String name;
                            public Run(String name) {
                                this.name=name;
                            }
                            public void run() {
                                for(int i=0;i<3;i++) {
                                    System.out.println(name+":"+i);
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        运行结果:
                        A:0
                        A:1
                        A:2
                        B:0
                        B:1
                        B:2
                        C:0
                        C:1
                        C:2

例三:通过Executors类静态方法ExecutorService newCachedThreadPool()(缓存线程)来创建线程池。再调用execute方法将任务添加至线程池后便可以运行。

                        import java.util.concurrent.ExecutorService;
                        import java.util.concurrent.Executors;
                        public class ThreadpoolDemo3 {
                            public static void main(String[] args) {
                                ExecutorService pool=Executors.newCachedThreadPool();
                                Run r1=new Run("r1");
                                Run r2=new Run("r2");
                                pool.execute(r1);
                                pool.execute(r2);
                                try {
                                    Thread.sleep(5000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                Run r3=new Run("r3");
                                Run r4=new Run("r4");
                                Run r5=new Run("r5");
                                pool.execute(r3);
                                pool.execute(r4);
                                pool.execute(r5);
                            }
                        }
                        class Run implements Runnable{
                            private String name;
                            public Run(String name) {
                                this.name=name;
                            }
                            public void run() {
                                for(int i=0;i<3;i++) {
                                    System.out.println(name+":"+i);
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        运行结果:
                        r2:0
                        r1:0
                        r1:1
                        r2:1
                        r2:2
                        r1:2
                        r4:0
                        r3:0
                        r5:0
                        r3:1
                        r4:1
                        r5:1
                        r3:2
                        r4:2
                        r5:2

                        可见在该方法中会根据先开始几个任务来确定给几个线程,给定线程后再运行接下来的任务,当开始的任务运行结束后,线程会处于空闲状态,当再次运行接下来的任务时,会同时开始确定数的任务,直至运行结束

例四:通过Executors类静态方法ScheduledExecutorService newScheduledThreadPool(int corePoolSize)(定时执行)来创建线程池。再调用execute方法将任务添加至线程池后便可以运行,该类还有一个方法为scheduled(Runnable command,long delay,TimeUnit unit),参数内容为什么任务,多长时间后执行,什么单位。

                        import java.util.concurrent.Executors;
                        import java.util.concurrent.ScheduledExecutorService;
                        import java.util.concurrent.TimeUnit;
                        public class ThreadpoolDemo4 {
                            public static void main(String[] args) {
                                ScheduledExecutorService pool=Executors.newScheduledThreadPool(2);
                                Run r1=new Run("A");
                                Run r2=new Run("B");
                                Run r3=new Run("C");
                                Run r4=new Run("D");
                                pool.schedule(r1,3,TimeUnit.SECONDS);
                                pool.execute(r2);
                                pool.execute(r3);
                                pool.schedule(r4,6,TimeUnit.SECONDS);
                            }
                        }
                        class Run implements Runnable{
                            private String name;
                            public Run(String name) {
                                this.name=name;
                            }
                            public void run() {
                                for(int i=0;i<3;i++) {
                                    System.out.println(name+":"+i);
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        运行结果:
                        B:0
                        C:0
                        B:1
                        C:1
                        B:2
                        C:2
                        A:0
                        A:1
                        A:2
                        D:0
                        D:1
                        D:2

猜你喜欢

转载自blog.csdn.net/z774884795/article/details/82905946