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;
        }
    }
  • 相关阅读:
    问题:charles开启Charles-Proxy-macOS Proxy 时报错
    通关中级测评师
    20210104 递归
    20201231-3 字符编码转换详解1
    20201231-2 元组
    20201231-1 购物车程序练习实例
    20201230-3 bytes数据类型
    20201230 python数据类型
    20201230 pyc是什么
    20201230-1 网络基础知识
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/11896695.html
Copyright © 2011-2022 走看看