• JAVA多线程笔试题


    一、题目内容

    二、我的答案

      利用了线程池、考虑了超时处理、不知道这样写是否还有其他问题,或者更好更优的解决方案?

    import java.util.*;
    import java.util.concurrent.*;
    
    public class Main {
    
        public static void main(String[] args) {
            List<String> allPaymentList=Arrays.asList("余额","红包","余额宝","银行卡");
            long start=System.nanoTime();
            List<String> list=filterDisablePayment(allPaymentList);
            double seconds=(System.nanoTime()-start)/1000000000.0;
            System.out.println("总共耗时:"+seconds+"s");
            for(String paymentType:list){
                System.out.println(paymentType);
            }
        }
    
    
        public static List<String> filterDisablePayment(List<String> allPaymentList){
            List<String> results=new ArrayList<>();
            ExecutorService executorService= Executors.newFixedThreadPool(allPaymentList.size());
            List<Future<String>> futures=new ArrayList<>();
            for(String paymentType:allPaymentList) {
                futures.add(executorService.submit(new PaymentMethodCallable(paymentType)));
            }
            executorService.shutdown();
            for(Future<String> future:futures){
                try {
                    //超时处理机制
                    String result= future.get(4,TimeUnit.SECONDS);
                    if(result!=null){
                        results.add(result);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return results;
        }
    
    
        public static Boolean PaymentIsEnabled(String paymentType) {
            try {
                //模拟远程服务调用所用时间
                Thread.sleep(3000);
                Random random=new Random();
                boolean result=random.nextBoolean();
                System.out.println("获取到结果:"+paymentType+":"+result);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    
    
        static class PaymentMethodCallable implements Callable<String> {
    
            private String paymentType;
    
            public String getPaymentType() {
                return paymentType;
            }
    
            public void setPaymentType(String paymentType) {
                this.paymentType = paymentType;
            }
    
            public PaymentMethodCallable(String paymentType) {
                this.paymentType = paymentType;
            }
    
            @Override
            public String call() {
                if(PaymentIsEnabled(paymentType)) return paymentType;
                return null;
            }
        }
    }
    

      


    作者:精彩
    出处:http://www.cnblogs.com/luodong/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    [RxJS] Filtering operators: throttle and throttleTime
    [RxJS] Transformation operators: debounce and debounceTime
    [AngularJS] Default Child state and nav between child state
    [RxJS] Transformation operator: bufferToggle, bufferWhen
    [RxJS] Transformation operators: delay and delayWhen
    [RxJS] Transformation operator: buffer, bufferCount, bufferTime
    [RxJS] Transformation operator: scan
    [置顶] 利用Global.asax的Application_Error实现错误记录,错误日志
    Java方法重写与方法重载
    Inside GDALAllRegister之一: 五大部分
  • 原文地址:https://www.cnblogs.com/luodong/p/9518084.html
Copyright © 2020-2023  润新知