• 线程池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++;
                }
                
                
    
            }
            
        }
    
    }
  • 相关阅读:
    解决IE新开窗口无法显示内容的问题
    日历控件
    关于索引,我们可以知道的更多 全表扫描和索引扫描
    C#构造函数的小说明
    动态创建的组件的ID设定
    正则表达式使用详解
    “运行”命令全集
    数组型参数
    接口实现的继承机制
    读取XML文件
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/9133974.html
Copyright © 2020-2023  润新知