title: Synchronized block on two threads in Swing not working
body: <p>Let me describe to you the context of my problem before I outline the issue. I'm currently in the middle of writing a game engine level editor and I'm working on the class that is going to act as the screen that the user interacts with in order to build their levels. I want to make it so the screen is proportional to the size of the editor. </p>
  
  <p>The issue in question occurs when I begin resizing my screen and drawing on it at the same time. I draw from one thread and at the same time I'm editing the size of the raw pixel array that I'm drawing onto, from another thread (the EDT). I know this is a big no-no so naturally, with no safety in place, I get the occasional IndexOutOfBounds Exception on a resize. </p>
  
  <p>My thought was, I could add a synchronize block on both the resizing code and the drawing code. This way, there would be no conflict and the issue should be avoided. However, the synchronization is being ignored completely. I'm still getting the same error and I'm really quite confused on why it isn't working. Below are the two methods of interest:</p>
  
  <pre><code>public void setPixel(int r, int g, int b, int x, int y) {
      synchronized (pixels){
          System.out.println("Start Draw...");
          int color = (r &lt;&lt; 16) | (g &lt;&lt; 8) | b;
          pixels[y * screenWidth + x] = color;
          System.out.println("End Draw...");
      }
  }
  
  
  @Override
  public void componentResized(ComponentEvent e) {
          synchronized (pixels) {
              System.out.println("Start resize");
              int width = e.getComponent().getWidth();
              int height = e.getComponent().getHeight();
              float aspectRatio = 4 / 3f;
              if (width &gt; height) {
                  width = (int) (height * aspectRatio);
              } else if (height &gt; width) {
                  height = width;
              }
              if (width &lt; 0 || height &lt; 0) {
                  width = 1;
                  height = 1;
              }
              this.screenWidth = width;
              this.screenHeight = height;
              image = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
              pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
              System.out.println("End Resize");
          }
      }
  </code></pre>
  
  <p>I don't know if it matters (it shouldn't right?) but my screen class extends a AWT Canvas. Also it is a listener on its parent component, so when that gets resized, it fires an event that triggers componentResized to be called. Anyway, thank you, any help is appreciated.</p>
  
  <p>EDIT: My drawing code can be found below.</p>
  
  <pre><code>new Thread(new Runnable(){
      @Override
      public void run() {
          while(true) {
              for (int y = 0; y &lt; screen.getHeight(); y++) {
                  for(int x = 0; x &lt; screen.getWidth(); x++){
                      int r = (int) (Math.random() * 255);
                      int g = (int) (Math.random() * 255);
                      int b = (int) (Math.random() * 255);
                      screen.setPixel(r, g, b, x, y);
                  }
              }
          }
      }
  }).start();
  </code></pre>