多线程案例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(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();
        }
    }
}

你可能感兴趣的:(JAVA相关资料)