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 not thread safe, AND this pattern of updating a count from multiple threads is probably the #1 way to achieve negative scaling (it runs slower when you add more threads) of a