Is a static counter thread safe in multithreaded application?

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前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准