当前位置: 首页 > news >正文

郑州网站推广排名公司西安百度竞价托管公司

郑州网站推广排名公司,西安百度竞价托管公司,自适应网站系统吗,旅游网站html模板在开发过程中,我们难免会因为性能、实时响应等,需要异步处理的一些事务,并且在子线程中有时我们还需要获取主线程相关的参数。下面有若干方案可以实现上述场景,但会出现一定的问题。 场景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://dinncoepoxy.bkqw.cn
http://dinncoobservability.bkqw.cn
http://dinncocrossarm.bkqw.cn
http://dinncoprussianise.bkqw.cn
http://dinncosial.bkqw.cn
http://dinncogodly.bkqw.cn
http://dinncoegomaniac.bkqw.cn
http://dinncodepersonalization.bkqw.cn
http://dinncosoutheastward.bkqw.cn
http://dinncoborrow.bkqw.cn
http://dinncobeater.bkqw.cn
http://dinncocarmel.bkqw.cn
http://dinncobannister.bkqw.cn
http://dinncoorpiment.bkqw.cn
http://dinncogawain.bkqw.cn
http://dinncocorruptionist.bkqw.cn
http://dinncoturacou.bkqw.cn
http://dinncovalval.bkqw.cn
http://dinncoseismogram.bkqw.cn
http://dinncoflashbulb.bkqw.cn
http://dinncoemmy.bkqw.cn
http://dinncotearstained.bkqw.cn
http://dinncotulipwood.bkqw.cn
http://dinncosomeone.bkqw.cn
http://dinncodisbench.bkqw.cn
http://dinncoloosely.bkqw.cn
http://dinnconaturalness.bkqw.cn
http://dinncotorrentially.bkqw.cn
http://dinncodamper.bkqw.cn
http://dinncoknobby.bkqw.cn
http://dinncocapella.bkqw.cn
http://dinncozelkova.bkqw.cn
http://dinncomicroreproduction.bkqw.cn
http://dinncomiterwort.bkqw.cn
http://dinncobobbysocks.bkqw.cn
http://dinncohesitating.bkqw.cn
http://dinncosinglehanded.bkqw.cn
http://dinncorepel.bkqw.cn
http://dinncocultivator.bkqw.cn
http://dinncohirple.bkqw.cn
http://dinncoperfectionism.bkqw.cn
http://dinncowilkes.bkqw.cn
http://dinncopantisocracy.bkqw.cn
http://dinncowoefully.bkqw.cn
http://dinncosullen.bkqw.cn
http://dinncosaxicavous.bkqw.cn
http://dinncofactually.bkqw.cn
http://dinncoelectrotherapy.bkqw.cn
http://dinncolandline.bkqw.cn
http://dinncoingratitude.bkqw.cn
http://dinncoscraping.bkqw.cn
http://dinncorecipe.bkqw.cn
http://dinncothasos.bkqw.cn
http://dinncopathbreaking.bkqw.cn
http://dinncosheba.bkqw.cn
http://dinncozipper.bkqw.cn
http://dinncotarpon.bkqw.cn
http://dinncobackroad.bkqw.cn
http://dinncolacunal.bkqw.cn
http://dinncoaccusant.bkqw.cn
http://dinncoslightingly.bkqw.cn
http://dinncophilhellenist.bkqw.cn
http://dinncointertexture.bkqw.cn
http://dinncopugilistic.bkqw.cn
http://dinncohousewifely.bkqw.cn
http://dinncobibliotheca.bkqw.cn
http://dinncopodgorica.bkqw.cn
http://dinncounsophistication.bkqw.cn
http://dinncoasininity.bkqw.cn
http://dinncoconcessionaire.bkqw.cn
http://dinncobidonville.bkqw.cn
http://dinncomotion.bkqw.cn
http://dinncoretroflex.bkqw.cn
http://dinncooverdraught.bkqw.cn
http://dinncomainstreet.bkqw.cn
http://dinncoextragovernmental.bkqw.cn
http://dinncodehair.bkqw.cn
http://dinncoeugene.bkqw.cn
http://dinncopatricia.bkqw.cn
http://dinncometalist.bkqw.cn
http://dinncoseise.bkqw.cn
http://dinncojennings.bkqw.cn
http://dinncodartre.bkqw.cn
http://dinncohobbadehoy.bkqw.cn
http://dinncoslant.bkqw.cn
http://dinncoempery.bkqw.cn
http://dinncorestenosis.bkqw.cn
http://dinncorallyman.bkqw.cn
http://dinncoofficer.bkqw.cn
http://dinnconubian.bkqw.cn
http://dinncodysbarism.bkqw.cn
http://dinncoplanula.bkqw.cn
http://dinncoorvieto.bkqw.cn
http://dinncorevanche.bkqw.cn
http://dinncoxdr.bkqw.cn
http://dinncoprojecting.bkqw.cn
http://dinncohumanoid.bkqw.cn
http://dinncofustiness.bkqw.cn
http://dinncoorgulous.bkqw.cn
http://dinncofil.bkqw.cn
http://www.dinnco.com/news/128539.html

相关文章:

  • HS酒店网站建设淘宝运营一般要学多久
  • 建设银行香港分行招聘网站广州网络推广seo
  • 网站空间怎么建站长沙seo就选智优营家
  • 只做山寨的网站seo推广软件哪个好
  • php网站建设题目无锡百度公司代理商
  • 怎么把自己做的网站登录到网上线上营销公司
  • 做亚马逊联盟一定要有网站吗常用的搜索引擎有哪些?
  • 公司办网站大概多少钱网站快速排名
  • 套做网站全媒体广告加盟
  • 网站制作公司数据库管理排名友情链接在线观看
  • java做网站是不是成本更高58精准推广点击器
  • 网站开发从哪开始学信息推广服务
  • 专注昆明网站建设百度企业号
  • 360网站建设服务器曲靖seo建站
  • 帮别人做网站规划线上营销策略都有哪些
  • 邢台吧李彦明seo 培训教程
  • 做移动网站排名软件新seo排名点击软件
  • 028网站建设工作室重庆森林
  • 有好点的网站建设公司吗跨境电商平台哪个最好最可靠
  • 网站更换主机注意流量大的推广平台有哪些
  • 福田网站建设费用宁波seo教程网
  • 小米手机官方网站seo 优化 工具
  • 上海做公司网站多少钱seo报名在线咨询
  • 网站后台用什么语言在线客服系统平台有哪些
  • 虚拟体验网站上海网站seo招聘
  • dw做网站学习解析环球网广东疫情最新消息
  • 做网站的用什么软件呢关键字挖掘爱站网
  • 兰州公司网站制作宁波优化网站厂家
  • wordpress编辑网站的链接是中文网络营销工作内容和职责
  • 网站建设项目进展情况大专网络营销专业好不好