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

Springmvc 异步处理的更多相关文章

  1. SpringMVC异步调用,Callable和DeferredResult的使用

    Callable和DeferredResult都是springMVC里面的异步调用,Callable主要用来处理一些简单的逻辑,DeferredResult主要用于处理一些复杂逻辑 1.Callabl ...

  2. SpringBoot+springmvc异步处理请求

    有两种情况,第一种是业务逻辑复杂,但不需要业务逻辑的结果,第二种是需要返回业务逻辑的处理结果 第一种比较简单,利用多线程处理业务逻辑,或者利用spring中@Asyn注解更简单, 使用@Asyn注解, ...

  3. 天天写同步,5种SpringMvc异步请求了解下!

    引言 说到异步大家肯定首先会先想到同步.我们先来看看什么是同步? 所谓同步,就是发出一个功能调用时,在没有得到结果之前,该调用就不返回或继续执行后续操作. 简单来说,同步就是必须一件一件事做,等前一件 ...

  4. Springmvc异步上传文件

    <script src="js/jquery.js" type="text/javascript"></script><scrip ...

  5. SpringMVC异步文件上传下载

    首先了解一下File的构造方法: File(String pathname):根据一个路径得到File对象 File(String parent,String child):根据一个目录和一个子文件/ ...

  6. SpringMVC 异步与定时使用示例

    1.Spring 的xml配置: <aop:aspectj-autoproxy/> <task:annotation-driven executor="annotation ...

  7. springmvc异步处理

    好久没有写过博客了,都是看大牛的文章,略过~~ 突然感觉成长在于总结!废话不多说,开干 由于是公司项目,所以不方便给出代码,看图操作 在项目util目录下创建工具类TaskExecutorConfig ...

  8. SpringMVC异步处理的 5 种方式

    作者:丁仪 来源:https://chengxuzhixin.com/blog/post/SpringMVC-yi-bu-chu-li-de-5-zhong-fang-shi.html 前段时间研究了 ...

  9. springmvc异步上传图片并回调页面函数插入图片url代码示例

    <tr> <td class="search_td">属性值图片值:</td> <td> <input type=" ...

随机推荐

  1. redis的cluster(分布式or分片)

    1.创建配置文件: /data/cluster目录下创建6个redis配置 2.逐个配置 port 7001 添加: daemonize yes cluster-enabled yes cluster ...

  2. 【原】命令行增删改查阿里云 DNS

    命令行解析阿里云 DNS 项目地址:https://github.com/liyongjian5179/alidns 首先需要获取阿里云账号账号的AccessKeyID及AccessKeySecret ...

  3. k8s创建harbor私有镜像仓库

    1. 部署准备 准备harbor软件包 在部署节点上: mv harbor-offline-installer-v1.4.0.tgz /opt/ && cd /opt tar zxvf ...

  4. Linux core dump总结

    文章链接:https://www.cnblogs.com/Anker/p/6079580.html 1.前言 一直在从事linux下后台开发,经常与core文件打交道.还记得刚开始从事linux下开发 ...

  5. Java并发编程-JUC-CountDownLatch 倒计数门闩器-等待多线程完成再放行 -一次性使用

    如题 (总结要点) CountDownLatch 倒计数门闩器, 让1-n-1个线程等待其他多线程完成工作. (Excel的多个Sheet的解析,最终等待解析完毕后;要实现主线程等待所有线程完成she ...

  6. js动画--链式运动

    前面几节我们只是讲述了一种运动,这节课我将讲述链式运动:就以一个动作接着一个动作完成. 对于这个实现,我们只需要改变一下就可以实现了,设置一个回调函数. var timer; window.onloa ...

  7. discuz x3.3后台admin.php防止直接恶意访问

    功能说明:admin.php是discuz默认的后台地址,正常情况下可以直接访问,为了防止某些恶意访问的情况,可以修改以下内容进行安全性能提升. 适用版本:Discuz!x1-x3.3 具体实施方案: ...

  8. BJOI2018 day2

    双人猜数游戏 Alice 和 Bob 是一对非常聪明的人,他们可以算出各种各样游戏的最优策略.现在有个综艺节目<最强大佬>请他们来玩一个游戏.主持人写了三个正整数 \(s\) .\(m\) ...

  9. SHOI2017 分手是祝愿

    分手是祝愿 有

  10. SpringBoot中实现Spring容器中注入类型相同但名不同Bean

    @Bean(autowire = Autowire.BY_NAME,value = "kaptchaProducer") public Producer kaptchaProduc ...