As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。




9年前关闭。




这是有关Java常见并发问题的各种民意测验。一个示例可能是经典的死锁或争用情况,或者是Swing中的EDT线程错误。我既对可能出现的问题的范围感兴趣,又对最常见的问题感兴趣。因此,请在每个评论中留下一个Java并发错误的特定答案,如果看到您遇到的评论,请投票。

最佳答案

我见过的最常见的并发问题是没有意识到不能保证一个线程写的字段可以被另一个线程看到。常见的用法是:

class MyThread extends Thread {
  private boolean stop = false;

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

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

只要stop不易失,或者setStoprun不同步,就不能保证它起作用。该错误特别令人讨厌,因为在99.999%的实践中这并不重要,因为读者线程最终将看到更改-但我们不知道他多久才能看到更改。

10-06 12:57