Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is that StringBuilder variable thread safe in this code?

Consider the below struts Action class in which, I am using a StringBuilder variable inside the execute method. My question: Is the variable sb threadsafe or not?

public DemoAction extends Action
{
    ......

    public ActionForward execute(.....)
    {
       StringBuilder sb = new StringBuilder();
    }
}

What if the same variable sb declared outside the execute(). Remember there will be only one object for DemoAction in WebContainer.?

like image 297
John Avatar asked May 18 '26 23:05

John


1 Answers

Local variables are thread safe, as long as no other thread somehow gets a reference to the same string builder instance, it’s thread safe.

like image 68
Konrad Rudolph Avatar answered May 21 '26 14:05

Konrad Rudolph