title: How to detect synchronization violations with Java
body: <p>I'm wondering what good ways there would be make assertions about synchronization or something so that I could detect synchronization violations (while testing).</p>
  
  <p>That would be used for example for the case that I'd have a class that is not thread-safe and that isn't going to be thread-safe. With some way I would have some assertion that would inform me (log or something) if some method(s) of it was called from multiple threads.</p>
  
  <p>I'm longing for something similar that could be made for AWT dispatch thread with the following:</p>
  
  <pre><code>public static void checkDispatchThread() {
      if(!SwingUtilities.isEventDispatchThread()) {
          throw new RuntimeException("GUI change made outside AWT dispatch thread");
      }
  }
  </code></pre>
  
  <p>I'd only want something more general. The problem description isn't so clear but I hope somebody has some good approaches =)</p>