Skip to main content
3 events
when toggle format what by license comment
Mar 29, 2013 at 15:56 comment added Steve Emmerson To prevent getTotal() from doing its job correctly, the second thread only needs to modify the "sb" argument that the first thread is using: the second thread doesn't need to call getTotal(). This is because StringBuilder isn't thread-safe; consequently, simultaneous access by multiple threads results in undefined behavior: calling "sb.toString()" in getTotal() might see all, some, or none of the changes from the second thread -- there's no a priori way to tell.
Mar 28, 2013 at 22:10 comment added sarath "In terms of doing its job correctly, however, getTotal() is not thread-safe if another thread modifies the "sb" argument." By this do you mean to say the sb argument being modified by an other thread which invoked the same static getTotal() method by passing a different StringBuffer argument and modifying it.In which case the results can be unpredictable which is hard to replicate.In such a scenario the results of one thread will be receieved wrongly by another thread
Mar 28, 2013 at 19:46 history answered Steve Emmerson CC BY-SA 3.0