多線程案例ThreadPoolsExecutor

package com.dream.web;

import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;
import java.util.concurrent.*;


/**
 * Created by test on 2019/5/30.
 */
public class ThreadPoolsTest {
    public static void main(String[] args) throws Exception {
       /* for(int i=0;i<5;i++) {

            MyThread myThread = new MyThread(i);
            new Thread(myThread).start();

        }*/

        //selfThreadPools();

        //cachedThreadPools();

        singleThreadPools();
    }

    /**
     *
     * @throws Exception
     */
    public static void singleThreadPools() throws Exception{
        ExecutorService threadPools = Executors.newSingleThreadExecutor();
        try {
            for (int i = 0; i < 1000; i++) {
                if(i%10==0){
                    Thread.sleep(1000);
                }
                final int j = i;
                threadPools.execute(new Runnable() {
                    @Override
                    public void run() {
                        //打印正在執行的緩存線程信息
                        System.out.println("-->" + Thread.currentThread().getName() + "正在被執行-->" + j);
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
            threadPools.shutdownNow();
        }finally {
            if(!threadPools.isShutdown()){
                threadPools.shutdown();
            }
        }
    }


    /**
     * 緩存線程池
     * @throws Exception
     */
   public static void cachedThreadPools() throws Exception{
       ExecutorService threadPools = Executors.newCachedThreadPool();
       try {
           for (int i = 0; i < 1000; i++) {
               final int j = i;
               threadPools.execute(new Runnable() {
                   @Override
                   public void run() {
                       //打印正在執行的緩存線程信息
                       System.out.println("-->" + Thread.currentThread().getName() + "正在被執行-->" + j);
                   }
               });
           }
       }catch (Exception e){
           e.printStackTrace();
           threadPools.shutdownNow();
       }finally {
           if(!threadPools.isShutdown()){
               threadPools.shutdown();
           }
       }
   }

    /**
     * 固定長線程池
     */
    public static void fixedThreadPools(final int n) throws Exception{
        System.out.println("==================>"+n);
        ExecutorService threadPools = Executors.newFixedThreadPool(3);
        threadPools.awaitTermination(5l, TimeUnit.SECONDS);
        for(int i=0;i<10;i++) {
            final int j=i;


            threadPools.execute(
                    new Runnable() {
                        @Override
                        public void run() {
                            //打印正在執行的緩存線程信息
                            System.out.println(n+"-->"+Thread.currentThread().getName() + "正在被執行-->"+j);
                        }
                    }
            );
        }

        if(!threadPools.isShutdown()){
            threadPools.shutdown();
        }
    }

    /**
     * 自定義線程池
     */
    public static void selfThreadPools(){
        ExecutorService threadPools = new ThreadPoolExecutor(2,4,0,TimeUnit.SECONDS,new LinkedBlockingDeque<Runnable>(100));
        try {
            for (int i = 0; i < 100; i++) {
                final int j = i;
                threadPools.execute(new Runnable() {
                    @Override
                    public void run() {
                        //打印正在執行的緩存線程信息
                        System.out.println("-->" + Thread.currentThread().getName() + "正在被執行-->" + j);
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
            threadPools.shutdownNow();
        }finally {
            if(!threadPools.isShutdown()){
                threadPools.shutdown();
            }
        }


    }
}


class  MyThread implements Runnable{
    private int j=0;
    public MyThread(int j){
        this.j = j;
    }
    public MyThread(){

    }
    @Override
    public void run() {
        try {
            System.out.println( "發起正在被執行-->"+j);
            ThreadPoolsTest.fixedThreadPools(j);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章