Is it safe for a Java servlet to spawn threads in order to satisfy a request?

后端 未结 5 1066
后悔当初
后悔当初 2020-12-11 10:01

Is it safe for my Java (Tomcat 8) web server to spawn threads in response to a HTTP request? I\'m seeing posts and forums where some people say it\'s absolutely fine, and ot

5条回答
  •  生来不讨喜
    2020-12-11 10:09

    Also, besides excellent answer of kuporific I really advise you to think if your result computation could be expressed in terms of map / filter / group operations on lists or maps, because in 90% of cases it's possible to do so.

    And if that's the case I would really advise you to use Java 8 Stream.parallelStream functionality as outlined in this official tutorial

    Please ask separate question if you are interested if/how it's possible to express your computation in that way

    Also, answering your initial question - it's perfectly fine to spawn threads to parallelise your computation anywhere (including servlets), however I would really advise to measure performance before and after optimization mainly because of reasons described in this superb article

提交回复
热议问题