怎么和网站合作推广,企业免费oa管理系统,东莞今天最新消息新闻,5年网站续费多少钱在开发过程中#xff0c;我们难免会因为性能、实时响应等#xff0c;需要异步处理的一些事务#xff0c;并且在子线程中有时我们还需要获取主线程相关的参数。下面有若干方案可以实现上述场景#xff0c;但会出现一定的问题。 场景1-基础场景
在主线程中开启子线程#x… 在开发过程中我们难免会因为性能、实时响应等需要异步处理的一些事务并且在子线程中有时我们还需要获取主线程相关的参数。下面有若干方案可以实现上述场景但会出现一定的问题。 场景1-基础场景
在主线程中开启子线程在子线程中获取主线程的参数。 重点子线程中逻辑处理时间较短在主线程结束前获取主线程的参数。
package com.lihao.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;/*** author lihao*/
RestController
RequestMapping(/test1)
public class Test1 {/*** 自定义线程池*/private ExecutorService executor new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),Runtime.getRuntime().availableProcessors(),5,TimeUnit.MINUTES,new LinkedBlockingQueue(100),Thread::new,new ThreadPoolExecutor.AbortPolicy());GetMapping(/asyncTest)public String asyncTest(HttpServletRequest request) {request.setAttribute(key1,value1);// 异步处理任务executor.submit(() - doExe(request));return OK;}public void doExe(HttpServletRequest request){System.out.println(值 request.getAttribute(key1));}
}执行结果
值value1我们可以正常拿到主线程的参数。
场景2-场景1的变种
在主线程中开启子线程在子线程中获取主线程的参数。 重点子线程在执行一段时间后再获取主线程的参数这个时候主线程已执行完成了。
GetMapping(/asyncTest)public String asyncTest(HttpServletRequest request) {request.setAttribute(key1,value1);// 异步处理任务executor.submit(() - doExe(request,1000L));return OK;}public void doExe(HttpServletRequest request,long sleepTime){try {Thread.sleep(sleepTime);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(值 request.getAttribute(key1));}
执行结果
值null由于子线程sleep了一秒这个时候主线程已经执行完成子线程如果想继续获取主线程的参数就会拿不到值。
场景3-场景1的完善
在主线程中开启子线程在子线程中获取主线程的参数。 重点子线程在执行一段时间后再获取主线程的参数主线程需要等待子线程执行完成后再结束。
GetMapping(/asyncTest)public String asyncTest(HttpServletRequest request) {request.setAttribute(key1,value1);// 异步处理任务Future? future executor.submit(() - doExe(request, 10000L));try {future.get();} catch (InterruptedException | ExecutionException e) {throw new RuntimeException(e);}return OK;}public void doExe(HttpServletRequest request,long sleepTime){try {Thread.sleep(sleepTime);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(值 request.getAttribute(key1));}虽然子线程执行时间较长但仍可以获取主线程的参数主线程在子线程执行完成后再结束。 主要技术通过future.get();来使主线程阻塞。 缺点主线程等待时间较长消息无法实时返回需要等待子线程执行完成后再返回。
场景4-场景1、2、3的优化
在主线程中开启子线程在子线程中获取主线程的参数。 重点子线程在执行一段时间后再获取主线程的参数主线程无需要等待子线程执行完成可立即结束。 GetMapping(/asyncTest)public String asyncTest(HttpServletRequest request) {request.setAttribute(key1,value1);// 开启异步AsyncContext asyncContext request.startAsync();executor.submit(() - doExe(asyncContext,request, 10000L));return OK;}public void doExe(AsyncContext asyncContext,HttpServletRequest request,long sleepTime){try {Thread.sleep(sleepTime);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(值 request.getAttribute(key1));asyncContext.complete();}虽然子线程执行时间较长但仍可以获取主线程的参数主线程无需等待子线程执行完成可立即返回。
核心技术点
开启异步 AsyncContext asyncContext request.startAsync();子线程执行完后调用 asyncContext.complete();
具体原理可阅读源码。
彩蛋
场景4在部分框架下失效如项目中引用Spring- Security框架等会导致主线程开启子线程后阻塞具体原因待分析。其他场景下可正常使用。 文章转载自: http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.mmtbn.cn.gov.cn.mmtbn.cn http://www.morning.qcfgd.cn.gov.cn.qcfgd.cn http://www.morning.yuanshenglan.com.gov.cn.yuanshenglan.com http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.pcjw.cn.gov.cn.pcjw.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.pwdrc.cn.gov.cn.pwdrc.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.qtqjx.cn.gov.cn.qtqjx.cn http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.qhczg.cn.gov.cn.qhczg.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.qjmnl.cn.gov.cn.qjmnl.cn http://www.morning.qmzwl.cn.gov.cn.qmzwl.cn http://www.morning.jfsbs.cn.gov.cn.jfsbs.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.kaoshou.net.gov.cn.kaoshou.net http://www.morning.nbiotank.com.gov.cn.nbiotank.com http://www.morning.wqbrg.cn.gov.cn.wqbrg.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.gkjyg.cn.gov.cn.gkjyg.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.fwmln.cn.gov.cn.fwmln.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.ftldl.cn.gov.cn.ftldl.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.nmnhs.cn.gov.cn.nmnhs.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn