• java线程池的应用


    package com.test;
    
    import java.util.concurrent.Callable;
    
    public class TestThread implements Callable<Object>{
    
        @Override
        public Object call() throws Exception {
            System.out.println(123);
            return 456;
        }
    
    }
    package com.test;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    public class TestPool {
    
        public void handleThreadPool(){
            int threadCount = 5;
            int poolSize = 3;
            TestThread[] tt = new TestThread[threadCount];
            ExecutorService service = Executors.newFixedThreadPool(poolSize);
            Collection<TestThread> c = new ArrayList<TestThread>();
            for (int i = 0; i < threadCount; i++) {
                tt[i] = new TestThread();
                c.add(tt[i]);
            }
            try {
                List<Future<Object>> results = service.invokeAll(c);
                service.shutdown();
                for (Future<Object> future : results) {
                     System.out.println(future.get().toString());
                }
            } catch (InterruptedException e) {            
                e.printStackTrace();
            } catch (ExecutionException e) {    
                e.printStackTrace();
            }        
        }
        
        public static void main(String[] args){
            TestPool tp = new TestPool();
            tp.handleThreadPool();
        }
    
    }
  • 相关阅读:
    c#缓存技术(Dictionary)
    反射Reflection创建
    SQL Server手注之延时型盲注
    MySQL——事务
    MySQL——NULL值处理
    MySQL——连接的使用
    SQL server手注之报错注入
    SQL Serves手注之联合查询注入
    MySQL手注之ROOT权限处理
    MySQL——正则表达式
  • 原文地址:https://www.cnblogs.com/zhangfei/p/3318837.html
Copyright © 2020-2023  润新知