Erklärung zum Java Code

inthemiddle

Mitglied
Hi. Ich muss ein Sequenzdiagramm zu einem Code erstellen, allerdings verstehe ich nichts. Wäre jemand so lieb und kann mir erklären was das genau macht?
z.B InterruptedException, waitUntilNotified sagen mir gar nichts..
Java:
public class A {

    private volatile boolean notified = false;
    private int z = -1;
    
    int f (int x) throws InterruptedException {
        C c = new C(x);
        Thread th = new Thread(() -> {c.g(11, A.this);});
        th.start();
        waitUntilNotified () ;
        return z;
    }
    
    private void waitUntilNotified() throws InterruptedException {
        do { Thread . sleep (50) ;} while ( notified == false ) ;
    }
    
    public void notify(int z1) {
        this.z = z1;
        this.notified = true;
    }
    
    public static void main(String[] args) {
        try {
            A a = new A();
            int r = a.f(42);
            System.exit(r);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

Java:
public class B{
    int x;
    B(int x) {
        this.x = x;
    }
    int h (int y) {
        return x+y;
    }

}

Java:
    public class C{
        B b;
        public C(int x) {
            b = new B(x);
        }
        void g (int y, A a) {
            int z = b.h(y);
            a.notify(z);
        }
    }

Vielen dank
 
Eine InterruptedException tritt auf wenn ein Thread während einer Activität und sonst wann beendet wurde. Also bevor seine Aufgabe beendet wurde 🙂

waitUntilNotified ist eine Methode welche eine in einer Schleife einen Thread warten lässt, bis die notify Flag auf true gesetzt wurde 😉
 

Zurück
Oben