Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task6;
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- public class Laufrad {
- private final Lock lock = new ReentrantLock();
- private final Condition man = lock.newCondition();
- private final Condition woman = lock.newCondition();
- private boolean isRunning = false;
- public void benutzen(Hamster hamster) throws InterruptedException {
- lock.lock();
- try {
- if (hamster.getGeschlecht() == Hamster.Geschlecht.Weiblich) {
- System.out.println(hamster.getGeschlecht() + " waiting");
- man.signal();
- woman.await();
- }
- if (hamster.getGeschlecht() == Hamster.Geschlecht.Maennlich) {
- System.out.println(hamster.getGeschlecht() + " waiting");
- woman.signal();
- man.await();
- }
- // hier rennt der Hamster im Laufrad
- long a = (new Double(Math.random()*1000)).longValue();
- Thread.sleep(a);
- System.out.println("Hamster im Laufrad: " + hamster.getGeschlecht());
- }
- finally { lock.unlock(); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement