zoukankan      html  css  js  c++  java
  • Springmvc 异步处理

    package com.lookcoder.haircutmember.controller.login.page.async;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.concurrent.*;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.context.request.async.DeferredResult;
    import org.springframework.web.context.request.async.WebAsyncTask;
    
    import java.util.concurrent.*;
    
    @RequestMapping("/async/")
    @Controller
    public class AsyncController {
    
        private static Logger log = LoggerFactory.getLogger(AsyncController.class);
        private static ExecutorService executor = Executors.newSingleThreadExecutor();
    
    
        @ResponseBody
        @RequestMapping("listenableFuture")
        public ListenableFuture<String> testdelistenableFuture() {
            log.info("in main thread.");
    //        Runnable r = () -> {
    //            for (int i = 0; i < 10; i++) {
    //                log.info("sub thread run.... " + (i + 1));
    //            }
    //            try {
    //                Thread.sleep(5 * 1000);
    //            } catch (InterruptedException e) {
    //                e.printStackTrace();
    //            }
    //        };
            Callable<String> r = () -> {
                int j = 0;
                for (int i = 0; i < 10; i++) {
                    j += i;
                    log.info("sub thread run.... " + (i + 1));
                }
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return String.valueOf(j);
            };
            ListenableFutureTask lf = new ListenableFutureTask(r);
    
            lf.addCallback(new ListenableFutureCallback() {
                @Override
                public void onFailure(Throwable ex) {
                    log.info("调用失败!");
                }
    
                @Override
                public void onSuccess(Object result) {
                    log.info("调用成功!" + result);
                }
            });
    
            executor.submit(lf);
    
            return lf;
        }
    
        @ResponseBody
        @RequestMapping("deferred")
        public DeferredResult<String> testdeferred() {
            DeferredResult<String> dr = new DeferredResult<>();
            executor.submit(() -> {
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int j = 0;
                for (int i = 0; i < 10; i++) {
                    log.info("children thread is run ...");
                    j+= i;
                }
                log.info("get value");
                boolean b = dr.setResult(String.valueOf(j));
                log.info("set value is ok!" + b);
            });
            return dr;
        }
    
        @ResponseBody
        @RequestMapping("runnable")
        public String testRunnable() {
            log.info("in main thread!");
            log.info("create sub thread!");
            Runnable c = () -> {
                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i;
                for (i = 0; i < 10; i++) {
                    log.info("sub thread task run...." + (i + 1));
                }
            };
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.submit(c);
            log.info("again in main thread!");
            try {
                Thread.sleep(10*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 10; i++) {
                log.info("main thread task run...." + (i + 1));
            }
            log.info("main thread is complete!");
            return "main thread return!";
        }
    
        @ResponseBody
        @RequestMapping("callable")
        public Callable<Integer> testCallable() {
            log.info("in main thread!");
            log.info("create sub thread!");
            Callable<Integer> c = () -> {
                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i;
                for (i = 0; i < 10; i++) {
                    log.info("sub thread task run...." + (i + 1));
                }
                return i;
            };
            log.info("again in main thread!");
            try {
                Thread.sleep(10*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 10; i++) {
                log.info("main thread task run...." + (i + 1));
            }
            log.info("main thread is complete!");
            return c;
        }
    
        @ResponseBody
        @RequestMapping("webAsyncTask")
        public WebAsyncTask<String> testWebAsyncTask() {
            log.info("in main thread!");
            log.info("create sub thread!");
            WebAsyncTask wat = new WebAsyncTask(() -> {
                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 10; i++) {
                    log.info("sub thread task run...." + (i + 1));
                }
                return "ok";
            });
            log.info("again in main thread!");
            try {
                Thread.sleep(10*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 10; i++) {
                log.info("main thread task run...." + (i + 1));
            }
            log.info("main thread is complete!");
            return wat;
        }
    }
  • 相关阅读:
    manacher(马拉车)算法详解+例题一道【bzoj3790】【神奇项链】
    【bzoj2160】【啦啦队排练】manacher(马拉车)+差分+快速幂
    file.seek()
    python strip() 函数探究
    vscode配置python环境
    关于ubuntu16.04系统无法系统更新的解决
    汇率计算器
    pythno学习小结-替换python字典中的key值
    python学习小结-字典key,val互换
    python学习笔记(7)——集合(set)
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/11896695.html
Copyright © 2011-2022 走看看