java基础

java基础

interface可以多继承

DTO中使用包装类 Integer,不要使用int, 防止set时出现NPE

xx.setXAmount(jsonObject.getInteger("returncount"));

使用spring mvc的异步线程 因为都会返回response,会调用Future对象的get方法,如果后台有sql异常是可以打印的.

但是会有一些异步处理的场景,使用了CompleteFuture.supplyAsync()又没有调用get方法,会造成异常不打印。如果确定不调用Future.get,最好还是使用没有返回值的ExecutorService.execute(Runnable),而不是submit方法

 1 public class FutureUtilTest {
 2 
 3 
 4     public static void main(String[] args) {
 5         MyPool myPool = new MyPool(2, 4, 0, TimeUnit.DAYS, new ArrayBlockingQueue(5555));
 6         FutureAsyncUtil.setExecutor(myPool);
 7 
 8         CompletableFuture<Integer> supply = FutureAsyncUtil.supply(() -> {
 9             System.out.println(1111);
10             if (1 == 1) {
11                 throw new IllegalArgumentException("000");
12             }
13             return 1;
14         });
15         /*try {
16             supply.get();//如果不get, 即使抛出异常,使用了afterExecute,也打印不出来
17         } catch (InterruptedException e) {
18             e.printStackTrace();
19         } catch (ExecutionException e) {
20             e.printStackTrace();
21         }*/
22 
23         FutureAsyncUtil.execute(()->{
24             System.out.println(3333);
25             if (1 == 1) {
26                 throw new IllegalArgumentException("3333");
27             }
28         });
29         myPool.shutdown();//没有shutdown jvm不会退出
30 
31         System.out.println("END");
32     }
33 }
34 
35 
36 class MyPool extends ThreadPoolExecutor {
37 
38     public MyPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
39         super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
40     }
41 
42     @Override
43     protected void afterExecute(Runnable r, Throwable t) {
44         super.afterExecute(r, t);
45         if (t != null) {
46             System.out.println("1111111" + t.getMessage());
47         }
48     }
49 }