激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring框架下向異步線程傳遞HttpServletRequest參數的坑

詳解Spring框架下向異步線程傳遞HttpServletRequest參數的坑

2021-07-24 14:56geekartt Java教程

這篇文章主要介紹了詳解Spring框架下向異步線程傳遞HttpServletRequest參數的坑,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在spring的注解 @requestmapping 之下可以直接獲取 httpservletrequest 來獲得諸如request header等重要的請求信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
@slf4j
@restcontroller
@requestmapping("/test")
public class testcontroller {
 
  private static final string header = "app-version";
 
  @requestmapping(value = "/async", method = requestmethod.get)
  public void test(httpservletrequest request) {
        request.getheader(header);
  }
}

往往,這些重要的信息也會在異步線程中被使用到。于是,一個很自然的想法是,那不如直接把這里獲取到的request當做參數傳到其它spawn出的子線程里,比如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@slf4j
@restcontroller
@requestmapping("/test")
public class testcontroller {
 
  private static final string header = "app-version";
 
  @requestmapping(value = "/async", method = requestmethod.get)
  public void test(httpservletrequest request) {
    log.info("main thread: " + request.getheader(header));  
    new thread(() -> {
      log.info("child thread: " + request.getheader(header));
    }).start();
  }
}

在header中設置"app-version"為1.0.1后發送 <base_url>/test/async 請求,可以看到結果:

main thread: 1.0.1
child thread: 1.0.1

但是,坑,也就此出現了。

由于 httpservletrequest 不是線程安全的(后知后覺),當主線程完成自己的工作返回response后,相應的諸如 httpservletrequest 等對象就會被銷毀。為了看到這個現象,我們可以在子線程中多等待一段時間來保證主線程先于子線程結束。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@slf4j
@restcontroller
@requestmapping("/test")
public class testcontroller {
 
  private static final string header = "app-version";
  private static final long child_thread_wait_time = 5000;
 
  @requestmapping(value = "/async", method = requestmethod.get)
  public void test(httpservletrequest request) {
    log.info("main thread: " + request.getheader(header));
 
    new thread(() -> {
      try {
        thread.sleep(child_thread_wait_time);
      } catch (throwable e) {
 
      }
      log.info("child thread: " + request.getheader(header));
    }).start();
  }
}

在header中設置"app-version"為1.0.1后發送 <base_url>/test/async 請求,可以看到結果:

main thread: 1.0.1
child thread: null

顯然,誰也沒辦法保證自己spawn出來的子線程會先于主線程結束,所以直接傳遞 httpservletrequest 參數給子線程是不可行的。

網上有一種方法是通過spring框架自帶的 requestcontextholder 來獲取request,這對異步線程來講是不可行的。因為只有在負責request處理的線程才能調用到 requestcontextholder 對象,其它線程中它會直接為空。

那么,一個可以想到的笨辦法是將request的值取出來,注入到自定義的對象中,然后將這個對象作為參數傳遞給子線程:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@slf4j
@restcontroller
@requestmapping("/test")
public class testcontroller {
 
  private static final string header = "app-version";
  private static final long main_thread_wait_time = 0;
  private static final long child_thread_wait_time = 5000;
 
  @requestmapping(value = "/async", method = requestmethod.get)
  public void test(httpservletrequest request) {
    log.info("main thread: " + request.getheader(header));
    testvo testvo = new testvo(request.getheader(header));
 
    new thread(() -> {
      try {
        thread.sleep(child_thread_wait_time);
      } catch (throwable e) {
 
      }
      log.info("child thread: " + request.getheader(header) + ", testvo = " + testvo.getappversion());
    }).start();
 
    try {
      thread.sleep(main_thread_wait_time);
    } catch (throwable e) {
 
    }
  }
 
  @data
  @allargsconstructor
  public static class testvo {
    private string appversion;
  }
}

再按照"app-version"為1.0.1發送請求后得到:

main thread: 1.0.1
child thread: null, testvo = 1.0.1

嗯,終于成功了。

故事似乎到此就結束了,但如果仔細考察細節的話,有幾個問題是值得思考的:

  • 如果child thread中的request已經被銷毀了,為什么沒有報null exception,而只是獲取到空的"app-version"的值?
  • 如果request被銷毀了,testvo這個同樣在主線程中創建的object為什么沒有被銷毀?
  • 主線程真的可以銷毀對象嗎?銷毀對象不是gc負責的嗎,為什么總是可以在child thread中得到null的結果?

一個合理的推理是:主線程結束時,調用了一個 destroy() 方法,這個方法主動將 httpservletrequest 中的資源釋放,例如調用了存放header的map對應的 clear() 方法。如此,在子線程中便無法獲得之前的"app-version"所對應的value了。而testvo由于是用戶自己創建,必然不可能實現在 destroy() 方法中寫出釋放資源的代碼。它的值也就保存下來了。

另外,無論主線程是否調用了 destroy() 方法,真正回收的時候還是gc的工作,這也就解釋了在子線程中不是報null exception,而只是取不到特定的key所對應的值。

進一步,我們還可以思考的問題是,為什么在主線程的 destoy() 方法中,不直接將request對象賦值為null呢?

這個問題看似有些蹊蹺,而實則根本不成立。因為就算你把主線程的request變量賦值為null時,子線程中的另一個變量已經指向了這個request對應的內存,依舊可以拿到相應的值。例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@slf4j
@restcontroller
@requestmapping("/test")
public class testcontroller {
 
  private static final string header = "app-version";
  private static final long main_thread_wait_time = 5000;
  private static final long child_thread_wait_time = 3000;
 
  @requestmapping(value = "/async", method = requestmethod.get)
  public void test(httpservletrequest request) {
    log.info("main thread: " + request.getheader(header));
    testvo testvo = new testvo(request);
 
    new thread(() -> {
      try {
        thread.sleep(child_thread_wait_time);
      } catch (throwable e) {
 
      }
      log.info("child thread: " + testvo.getrequest().getheader(header));
    }).start();
 
    request = null;
 
    try {
      thread.sleep(main_thread_wait_time);
    } catch (throwable e) {
 
    }
  }
 
  @data
  @allargsconstructor
  public static class testvo {
    private httpservletrequest request;
  }
}

按照"app-version"為1.0.1發送請求后得到:

main thread: 1.0.1
child thread: 1.0.1

這里讓子線程等待3秒,以便主線程有充分的時間將request賦值為null。但child線程依舊可以拿到對應的值。

所以,將request變量賦值為null根本無法做到釋放資源。所以對request里保存header的map來講,將變量賦值為null無法保證其它地方的引用也會一并消失。最直接有效的方法是調用 clear() 讓map中的每一個元素失效。

所以總結起來是:

  • 主線程的request和testvo,由于都有子線程的變量指向,也即是兩個對象上的reference count不為0,gc便不會真正回收這兩部分對應的內存。
  • 但是,由于request很可能在主線程中在 destroy() 方法被調用了內部map的 clear() 方法,導致無法獲取到header的值。
  • testvo是用戶創建的對象,無法事先被放到 destroy() 方法中被釋放,所以還能繼續保持原有的值。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000018593620

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久国产精品久久精品国产演员表 | 中文字幕在线观看二区 | 性欧美xxxx免费岛国不卡电影 | 成人做爰www免费看 成人午夜视频免费看 | 久久亚洲精品视频 | 日本视频在线免费观看 | 男女羞羞视频在线观看免费 | 激情综合视频 | 亚洲91网站 | 色综合久久久久久久久久 | 久久久久久久91 | 欧美人与牲禽动交精品一区 | av色先锋| www国产网站 | 国产精品久久av | 久久成人激情视频 | 日韩字幕在线观看 | 男女污视频在线观看 | 成人午夜在线免费视频 | 91情侣偷在线精品国产 | 国毛片| 久久久久久久久久美女 | 成人不卡在线观看 | 色999国产| 亚洲综合中文 | 欧美一级一区二区三区 | 国产亚洲精品久久久久久久久 | 国产成人免费高清激情视频 | 色人阁导航 | 欧美黄色一级片在线观看 | 羞羞视频2023| 91情侣在线偷精品国产 | 亚洲午夜天堂吃瓜在线 | 91网视频在线观看 | 中国3xxxx | 老司机一级毛片 | 久久出精品| 婷婷一区二区三区 | 性少妇videosexfreexx| 91精品国产91久久久久久不卞 | 一区二区三区欧美在线观看 |