2012-04-04 12 views
0

Wie schreibe ich eine einfache faire Sperre analog zu neuen ReentrantLock(true)?Wie schreibt man ein einfaches Fair-Lock?

 public class Main1 { 

    public static void main(String[] args) { 
//  Lock lock = new ReentrantLock(true); 
     CustomLock lock = new CustomLock(); 
     new Thread(new Producer(lock)).start(); 
     new Thread(new Consumer(lock)).start(); 
    } 
} 

class Producer implements Runnable { 
    private Lock lock; 
    private CustomLock customLock; 

    public Producer(Lock lock) { 
     this.lock = lock; 
    } 

    public Producer(CustomLock lock) { 
     this.customLock = lock; 
    } 

    @Override 
    public void run() { 
     while (!Thread.currentThread().isInterrupted()) { 
//   lock.lock(); 
      customLock.lock(); 
      System.out.println("Producer before"); 
      try { 
       TimeUnit.SECONDS.sleep(1); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      System.out.println("Producer after"); 
//   lock.unlock(); 
      customLock.unlock(); 
     } 
    } 
} 

class Consumer implements Runnable { 
    private Lock lock; 
    private CustomLock customLock; 

    public Consumer(Lock lock) { 
     this.lock = lock; 
    } 

    public Consumer(CustomLock lock) { 
     this.customLock = lock; 
    } 

    @Override 
    public void run() { 
     while (!Thread.currentThread().isInterrupted()) { 
//   lock.lock(); 
      customLock.lock(); 
      System.out.println("Consumer before"); 
      try { 
       TimeUnit.SECONDS.sleep(1); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      System.out.println("Consumer after"); 
//   lock.unlock(); 
      customLock.unlock(); 
     } 
    } 
} 

class CustomLock{ 
    private boolean isLocked; 

    public synchronized void lock(){ 
     while (isLocked) { 
      try { 
       wait(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     isLocked = true; 
    } 

    public synchronized void unlock(){ 
     if(isLocked){ 
      isLocked = false; 
      notify(); 
     } 
    } 
} 

Gewohnheit nicht fair Sperre (Ich bin nicht sicher, dass es richtig ist)

class CustomLock{ 
    private boolean isLocked; 

    public synchronized void lock(){ 
     while (isLocked) { 
      try { 
       wait(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     isLocked = true; 
    } 

    public synchronized void unlock(){ 
     if(isLocked){ 
      isLocked = false; 
      notify(); 
     } 
    } 
} 
+0

Was ist los mit Frage? – user1074896

Antwort

1

Wenn Sie eine faire Sperre benötigen Sie eine Liste verwenden und benachrichtigen Fäden nach der Reihenfolge der Liste.

Verwandte Themen