StringBuffer or StringBuilder in Servlet's doFilter method?

This question already has an answer here:

  • Difference between StringBuilder and StringBuffer 31 answers

  • Local variables are thread-safe and variables declared inside the doFilter() method will be thread safe. Use StringBuilder for your purpose, as you shouldn't unnecessarily incur the overhead of synchronization used in StringBuffer .

    Moreover , The Servlet request and response objects are created afresh for every new request and response and so by their nature they are thread safe. The doFilter() method will be executed in separate threads for each request.

    Suggested Reading:

  • Why are local variables thread safe in Java.
  • StringBuilder and StringBuffer in Java
  • servlet-filters tag wiki

  • 由于它将是一个局部变量并且不被线程共享,所以可以使用StringBuilder。


    StringBuilder is a better choice as it is faster because of its non-synchronized nature. ServletFilter doFilter for each request run in its own thread so you don;t need a synchronized datastructure. And if it is simply a local varibale then StringBuilder is the choice.

    链接地址: http://www.djcxy.com/p/72338.html

    上一篇: StringBuilder和Stringbuffer有什么区别?

    下一篇: Servlet的doFilter方法中的StringBuffer或StringBuilder?