Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. package task6;
  2.  
  3. import java.util.concurrent.locks.Condition;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;
  6.  
  7. public class Laufrad {
  8.    
  9.     private final Lock lock = new ReentrantLock();
  10.     private final Condition man = lock.newCondition();
  11.     private final Condition woman = lock.newCondition();
  12.     private boolean isRunning = false;
  13.    
  14.    
  15.     public void benutzen(Hamster hamster) throws InterruptedException {
  16.         lock.lock();
  17.             try {
  18.                 if (hamster.getGeschlecht() == Hamster.Geschlecht.Weiblich) {
  19.                     System.out.println(hamster.getGeschlecht() + " waiting");
  20.                     man.signal();
  21.                     woman.await();
  22.                 }
  23.                 if (hamster.getGeschlecht() == Hamster.Geschlecht.Maennlich) {
  24.                     System.out.println(hamster.getGeschlecht() + " waiting");
  25.                     woman.signal();
  26.                     man.await();
  27.                 }
  28.  
  29.                 // hier rennt der Hamster im Laufrad
  30.                 long a = (new Double(Math.random()*1000)).longValue();
  31.                 Thread.sleep(a);
  32.                 System.out.println("Hamster im Laufrad: " + hamster.getGeschlecht());
  33.             }
  34.             finally { lock.unlock(); }
  35.  
  36.            
  37.        
  38.        
  39.        
  40.        
  41.        
  42.        
  43.  
  44.        
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement