更新時間:2022-12-26 15:23:59 來源:動力節點 瀏覽1670次
筆試題--并發設計
場景貓達:用戶在支付寶擁有多種支付方式余額、紅包、余寶等,每種支付工具分布在不同系統),每種支付方式通過調用遠程服務獲取可用性。在外部資源環境不變情況下,請設計程房以最通響應時間得盡可能多的可用支付方式列表。
補充:假定支付方式可用性咨詢接口統一為
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官網。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習