Universität Paderborn - Home Universität Paderborn
Die Universität der Informationsgesellschaft

Parallel Programming WS 2014/2015 - File OneLane.java

public class OneLane {
    int nb; // northbound cars on bridge 
     int sb; // southbound cars on bridge 
   
    synchronized public void enter_sb() {
        while (nb != 0) {
            try {
                wait();
            } catch (InterruptedException ie) {/* ok to ignore */}
        }
        sb++;
        notifyAll();
        System.out.println("sb enter: " + sb + " southbound cars on lane");
    }
   
    synchronized public void leave_sb() {
        while (sb == 0) {
            try {
                wait();
            } catch (InterruptedException ie) {/* ok to ignore */}
        }
        sb--;
        System.out.println("sb leave: " + sb + " southbound cars on lane");
        if (sb == 0) {
            notifyAll();
        }
    }

    synchronized public void enter_nb() {
        while (sb != 0) {
            try {
                wait();
            } catch (InterruptedException ie) {/* ok to ignore */}
        }
        nb++;
        notifyAll();
        System.out.println("nb enter: " + nb + " northbound cars on lane");
    }
   
    synchronized public void leave_nb() {
        while (nb == 0) {
            try {
                wait();
            } catch (InterruptedException ie) {/* ok to ignore */}
        }
        nb--;
        System.out.println("nb leave: " + nb + " northbound cars on lane");
        if (nb == 0) {
            notifyAll();
        }
    }
}

Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 03.12.2014