public class counting
{
private static int counter = 0;
public void boolean counterCheck(){
counter++;
if(counter==10)
counter=0;
}
}
Method counterCheck can be accessed by multiple threads in my application. I know that static variables are not thread safe. I would appreciate if someone can help me with example or give me reason why I have to synchronize method or block. What will happen if I don't synchronize? This is actually close to the quintessential example of code that is not threadsafe.
以上就是Is a static counter thread safe in multithreaded application?的详细内容,更多请关注web前端其它相关文章!