Is a static counter thread safe in multithreaded application?

后端 未结 6 681
青春惊慌失措
青春惊慌失措 2021-01-14 13:02
public class counting
{
  private static int counter = 0;

  public void boolean counterCheck(){
  counter++;
  if(counter==10)
  counter=0;
  }
}

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 13:35

    Biggest danger? Two increments to counter before the counter == 10 check, making the reset to 0 never happen.

提交回复
热议问题