线程池的爆掉

  看例子:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest
{
    // 定义个固定大小为64个线程的线程池
    private final ExecutorService es = Executors.newCachedThreadPool();
    
    public static void main(String[] args)
    {
        ThreadPoolTest t = new ThreadPoolTest();
        t.poolTest(10000, 4, 1);
    }
    
    /**
     * 测试不同线程数量对线程池的冲击
     * 
     * @author wulinfeng
     * @param nThread 每次投入多少线程到线程池中
     * @param nSecondDelay 每个任务的请求数
     * @param nSecondDelay 在每个任务中休眠多少秒
     */
    public void poolTest(int nThread, int nRequest, int nSecondDelay)
    {
        // 创建任务列表
        List<SubFutureTask<String>> tasks = new ArrayList<SubFutureTask<String>>();
        
        // 创建任务,放入任务列表
        for (int i = 0; i < nThread; i++)
        {
            Callable<String> callable1 = new ResultCallable<String>(nRequest, nSecondDelay);
            SubFutureTask<String> task1 = new SubFutureTask<String>(callable1);
            tasks.add(task1);
        }
        
        // 从任务列表中获取执行结果
        String result = getSubmitResult(tasks);
        System.out.println(result);
    }
    
    /**
     * 获取结果集
     * 
     * @author wulinfeng
     * @param tasks
     * @return
     */
    private String getSubmitResult(List<SubFutureTask<String>> tasks)
    {
        // 循环遍历出任务对象并提交到Executor
        if ((null != tasks && tasks.size() > 0))
        {
            for (int i = 0; i < tasks.size(); i++)
            {
                if (null != es && !es.isShutdown())
                {
                    es.submit(tasks.get(i));
                }
            }
        }
        
        // 从提交任务到执行,线程池需要一定时间做准备
        try
        {
            Thread.sleep(10000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        
        return (String)getResult(tasks);
    }
    
    /**
     * 包装结果
     * 
     * @author wulinfeng
     * @param tasks
     * @return
     */
    private String getResult(List<SubFutureTask<String>> tasks)
    {
        StringBuilder sb = new StringBuilder();
        if ((null != tasks && tasks.size() > 0))
        {
            for (int i = 0; i < tasks.size(); i++)
            {
                SubFutureTask<String> task = tasks.get(i);
                String result = null;
                if (null != task)
                {
                    try
                    {
                        // 使用超时时间限制,超时则抛异常
                        Object res = task.get(3000, TimeUnit.MILLISECONDS);
                        if (res instanceof String)
                        {
                            result = (String)res;
                        }
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                
                if (result != null)
                {
                    sb.append(String.format("第%d个线程:\n", i));
                    sb.append(result).append('\n');
                }
            }
        }
        return sb.toString();
    }
    
    /**
     * 定义任务内容
     * 
     * @author wulinfeng
     * @version C10 2018年9月3日
     * @since SDP V300R003C10
     */
    class ResultCallable<T> implements Callable<T>
    {
        private int nRequest;
        
        private int nSecondDelay;
        
        public ResultCallable(int nRequet, int nSecondDelay)
        {
            this.nRequest = nRequet;
            this.nSecondDelay = nSecondDelay;
        }
        
        @Override
        public T call()
            throws Exception
        {
            StringBuilder sb = new StringBuilder();
            
            for (int i = 0; i < nRequest; i++)
            {
                // 该请求耗时nSecondDelay秒
                try
                {
                    Thread.sleep(1000 * nSecondDelay);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                sb.append(String.format("第(%d)次请求,获取响应:%d\n", i, System.currentTimeMillis()));
            }
            return (T)sb.toString();
        }
    }
    
    /**
     * 获取callable对象
     * 
     * @author wulinfeng
     * @version C10 2018年9月3日
     * @since SDP V300R003C10
     */
    class SubFutureTask<V> extends FutureTask<V>
    {
        private Callable<V> callable;
        
        public SubFutureTask(Callable<V> arg0)
        {
            super(arg0);
            this.callable = arg0;
        }
        
        public Callable<V> getCallable()
        {
            return this.callable;
        }
        
    }
}

猜你喜欢

转载自www.cnblogs.com/wuxun1997/p/9582290.html
今日推荐