2013-05-02 4 views
1

Je ne suis pas familier avec les threads et la programmation simultanée. Je cherchais un extrait simple, qui entraînerait une impasse, la voici:Java synchronisé vs exemple de blocage

public class TestLock { 
    private static class fun { 
     int a,b; 

     void read() {System.out.println(a+b);} 
     void write(int a,int b) {this.a=a;this.b=b;} 
    } 

    public static void main (String[] args) throws java.lang.Exception { 
     final fun d1=new fun(); 
     final fun d2=new fun(); 

     Thread t1=new Thread() { 
      public void run() { 
       for(int i=0;i<5;i++) { 
        synchronized(d2) { 
         d2.read(); 
         try { 
          Thread.sleep(50); 
         } catch (Exception ex) { 
          ex.printStackTrace(); 
         } 
         synchronized(d1) { 
          d1.write(i, i); 
        } 
       } 
      } 
     }; 

     Thread t2=new Thread() { 
      public void run() { 
       for(int i=0;i<5;i++) { 
        synchronized(d1) { 
         d1.read(); 
         try { 
          Thread.sleep(50); 
         } catch (Exception ex) { 
          ex.printStackTrace(); 
         } 
         synchronized(d2) { 
          d2.write(i, i); 
         } 
        } 
       } 
      } 
     }; 
     t1.start(); 
     t2.start(); 
    } 
} 

Maintenant, je me demandais comment je pouvais transformer cet exemple, en utilisant ReentrantLock au lieu de synchronisation, mais je ne vois pas comment: t-il besoin amusant d'avoir un attribut ReentrantLock afin d'avoir quelque chose comme

Thread t1=new Thread() { 
    public void run() { 
     for(int i=0;i<5;i++) { 
      if(d2.lock.tryLock()) { 
        try {d1.read();Thread.sleep(50);} catch(Exception e) {e.printStackTrace();} finally {d1.lock.unlock();} 
         if(d2.lock.tryLock()) { 
          try {d2.write(i, i);} catch(Exception e) {e.printStackTrace();} finally {d2.lock.unlock();} 
         } 
        } 
       } 
      } 
     }; 

ou suis-je manque quelque chose tout à fait?

Répondre

1

La transformation de l'exemple en utilisant ReentrantLocks signifierait en effet l'utilisation de deux verrous: un associé à d1 et l'autre associé à d2.

Et vous devez remplacer chaque entrée dans un bloc synchronisé sur dX par un appel à lockX.lock(), et toute sortie d'un bloc synchronisé sur dX par un appel à lockX.unlock() `.

L'utilisation de tryLock() annule le but, car elle renvoie au lieu d'attendre si le verrou ne peut pas être acquis.