what's problem will you face in the multiple threading tasks

Posted apanda009

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了what's problem will you face in the multiple threading tasks相关的知识,希望对你有一定的参考价值。

The most common concurrency problem I‘ve seen, is not realizing that a field written by one thread is not guaranteed to be seen by a different thread. A common application of this:

class MyThread extends Thread {
  private boolean stop = false;

  public void run() {
    while(!stop) {
      doSomeWork();
    }
  }

  public void setStop() {
    this.stop = true;
  }
}

As long as stop is not volatile or setStop and run are not synchronized this is not guaranteed to work. This mistake is especially devilish as in 99.999% it won‘t matter in practice as the reader thread will eventually see the change - but we don‘t know how soon he saw it.

以上是关于what's problem will you face in the multiple threading tasks的主要内容,如果未能解决你的问题,请参考以下文章