zoukankan      html  css  js  c++  java
  • 线程池demo

    package test;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    public class Test1 {
        public static void main(String[] args) {
            test2();
        }
        
        public static void test1(){
            int count = Runtime.getRuntime().availableProcessors();
            ExecutorService service = Executors.newFixedThreadPool(count);
            
            A a = new A();
            
            try {
                for(int i = 0;i<count;i++){
                    service.execute(a);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                if(!service.isShutdown()){
                    service.shutdown();
                }
            }
        }
        
        public static void test2(){
            int count = Runtime.getRuntime().availableProcessors();
            ExecutorService service = Executors.newFixedThreadPool(count);
            
            B b = new B();
            
            List<Future<String>> list = new ArrayList<Future<String>>();
            try {
                for(int i = 0;i<count;i++){
                    Future<String> f = service.submit(b);
                    list.add(f);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                if(!service.isShutdown()){
                    service.shutdown();
                }
            }
            
            try {
                String a = list.get(0).get();
                System.out.println("a:"+a);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            
            System.out.println("next");
            
            
        }
    }
    
    
    class B implements Callable<String>{
    
        @Override
        public String call() throws Exception {
            String a = "call";
            Thread.sleep(10000);
            System.out.println(a);
            return a;
        }
    
    
        
    }
    class A implements Runnable {
        private int ticketNum = 1;
        private Object o = new Object();
        
        @Override
        public void run() {
            
            while(true){
                
                synchronized (o) {
                    if(ticketNum>500000){
                        break;
                    }
                    
                    System.out.println(Thread.currentThread().getName()+"出售了第"+ticketNum+"张票");
                    
                    ticketNum++;
                }
                
                
    
            }
            
        }
    
    }
  • 相关阅读:
    腾讯云挂载文件服务器节点
    OpsManage 安装
    centos7 安装mysql
    vs code 新建vue项目
    Centos7 安装supervisor
    腾讯云Centos7 安装nginx
    django 生成pdf
    VM安装虚拟机
    ACM/ICPC 之 Floyd练习六道(ZOJ2027-POJ2253-POJ2472-POJ1125-POJ1603-POJ2607)
    ACM/ICPC 之 Floyd范例两道(POJ2570-POJ2263)
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/9133974.html
Copyright © 2011-2022 走看看