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

最新文章

  1. lua 字符串 正则表达式 转义 特殊字符
  2. hdu4790 Just Random (数学?)
  3. JVM学习笔记:JVM的体系结构与JVM的生命周期
  4. signed char、unsigned char
  5. innobackupex 单脚本循环7天一全备6增备脚本更新
  6. zoj3658 Simple Function (函数值域)
  7. Linux下 输入 env 而得到的环境变量解读
  8. ArcGIS for Silverlight 地图卷帘
  9. Zabbix简介及安装
  10. Python3+unitest自动化测试初探(下篇)
  11. 题解 CF934A 【A Compatible Pair】 ——贪心
  12. [数]数学系列预习-&gt;补水题ver.
  13. python selenium 基本常用操作
  14. ServerSocket详解及线程阻塞_03
  15. 交叉熵理解:softmax_cross_entropy,binary_cross_entropy,sigmoid_cross_entropy简介
  16. MyBatis源码解析(四)——DataSource数据源模块
  17. [书摘]图解HTTP 状态码
  18. WebApi中的参数传递
  19. Ubuntu 14.04 LTS 安装Docker(转)
  20. Python 学习笔记(九)Python元组和字典(二)

热门文章

  1. 大数据:Hadoop(HDFS 的设计思路、设计目标、架构、副本机制、副本存放策略)
  2. 【异常】Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30005ms.
  3. 一款信息收集工具-theHarvester
  4. mysql分析sql语句基础工具 -- explain
  5. 命令行的方式启动和关闭Mysql
  6. java.util. Arrays.sort(scores);
  7. OpenGL是什么?GPU是什么?
  8. matlab基础向1-6:基础语法
  9. python-随机生成验证码实例
  10. Git git2.8.1客户端安装教程