zoukankan      html  css  js  c++  java
  • 多线程计算0-100 0-200 的和

    va.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    public class Demo3 {
        //异步计算0-100的和
        //跟0-200的和
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            //获取线程池
            ExecutorService es=Executors.newFixedThreadPool(2);
            //创建callable子类
            JiSuan js=new JiSuan(100);
            JiSuan js2=new JiSuan(200);
            //提交callable子类
            Future<Integer> f1=es.submit(js);
            Future<Integer> f2=es.submit(js2);
            //从future中获取返回值
            System.out.println(f1.get());
            System.out.println(f2.get());
            //关闭线程池
            es.shutdown();
        }
    
    }
    import java.util.concurrent.Callable;
    
    public class JiSuan  implements Callable<Integer>{
        private Integer a ;
        JiSuan(Integer a){
            this.a=a ;
        }
        public Integer call(){
            int sum=0;
            for(int i=0;i<a;i++){
                sum+=i;
            }
            return null;
        }
    }
  • 相关阅读:
    Javascript高性能编程-提高Dom访问速度
    获取在线人数
    js倒计时
    支付宝支付
    微信扫码支付
    多条sql语句实现事物处理
    防止页面重复刷新
    bootstrapTable.js 使用
    org_chart.js 使用方法
    js上传图片及时显示
  • 原文地址:https://www.cnblogs.com/a709898670/p/9540608.html
Copyright © 2011-2022 走看看