码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-30 23:36:50      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:ati   zed   end   syn   app   multiple   task   ade   enc   

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

标签:ati   zed   end   syn   app   multiple   task   ade   enc   

原文地址:http://www.cnblogs.com/apanda009/p/7932224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!