专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 职业指南 2023最新的Java多线程笔试题

2023最新的Java多线程笔试题

更新时间:2022-12-26 15:23:59 来源:动力节点 浏览1148次

笔试题--并发设计

场景猫达:用户在支付宝拥有多种支付方式余额、红包、余宝等,每种支付工具分布在不同系统),每种支付方式通过调用远程服务获取可用性。在外部资源环境不变情况下,请设计程房以最通响应时间得尽可能多的可用支付方式列表。

补充:假定支付方式可用性咨询接口统一为
ConsultResult PaymentRemot eSerivce.isEnabled(String paymentType):
返回结果
public class ConsultResult {
/** 咨询结果是否可用*/
private boolean isEnable:
/**错误码 */
private String errorCode:
**
*过滤不可用支付方式类型
*@aram payment TypeList 原始支付方式类型列表*@return 可用支付方式类型列表
*
public List<String> filterDisablePayment (List<String> allPaymentList)[//: TODO 完成此处的代码

答案

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

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;
        }
    }
}

以上就是“2023最新的Java多线程笔试题”,你能回答上来吗?如果想要了解更多的Java面试题相关内容,可以关注动力节点Java官网。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>