<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
public class Interblocage {
  public static void main(String[] args) {
    final int[] tab1 = { 1, 2, 3, 4 };
    final int[] tab2 = { 0, 1, 0, 1 };
    /* tab1 et tab2 sont susceptibles d'etre modifiÃ©s 
       par d'autres threads */
    final int[] tabAdd = new int[4];
    final int[] tabSub = new int[4];

    // rÃ©alise l'addition tabAdd = tab1 + tab2
    Thread tacheAdd = new Thread() {
      public void run() {
        synchronized(tab1) {
          System.out.println("Thread tacheAdd lock tab1");
          travailHarassant();
          synchronized(tab2) {
            System.out.println("Thread tacheAdd lock tab2");
            for (int i=0; i&lt;4 ; i++)
              tabAdd[i] = tab1[i] + tab2[i];
          }
        }
      }
    };
    // rÃ©alise la soustraction tabAdd = tab1 - tab2
    Thread tacheSub = new Thread() {
      public void run() {
        synchronized(tab2) {
          System.out.println("Thread tacheSub lock tab2");
          travailHarassant();
          synchronized(tab1) {
            System.out.println("Thread tacheSub lock tab1");
            for (int i=0; i&lt;4 ; i++)
              tabAdd[i] = tab1[i] - tab2[i];
          }
        }
      }
    };
    tacheAdd.start(); 
    tacheSub.start();
  }
  static void travailHarassant() {
    try { 
      Thread.sleep((int)(Math.random()*50+25));
    }
    catch (InterruptedException e) {}
  }
}
</pre></body></html>