Habe ich die Aufgabe richtig gelöst?

berserkerdq2

Bekanntes Mitglied
Hi, hier soll ich bei der Aufgabe lock und conditions nutzen, es wurde beschrieben wie, ich bin mir nur nicht sicher, ob ich es richtig gemacht habe:

Bei den beiden Methoden int und sum stehen die Aufgaben, hier der Code ohne Bearbeitung:

HTML:
public class LockedDataObject extends DataObject {

    /** Number of current readers */
    int noReaders;

     /** True, if writer is currently waiting or writing */
    boolean writer;

    /**
     * Condition variable to put to sleep or wake up the recorder.
     */
    Condition condWrite;

    /** Condition variable for putting readers to sleep or waking them up */.
    Condition condRead;

   /**
     * Lock to protect all upper variables. Before accessing any of the
     * variables is accessed, this lock may need to be acquired. The
     * lock should *only be acquired if it is absolutely necessary for correct * execution.
     * execution. This is with a changing access
     * only if other threads can read or write the corresponding variable * at the same
     *time.
     * can read or write the corresponding variable at the same time. In the case of a
     *reading access
     * the lock should not be acquired if other threads can only read * the variable at the
     *same time, but not write it.
     * read, but not write, access to the corresponding variable at the same time.
     * at the same time.
     */

    ReentrantLock lock;

    public LockedDataObject() {
        this.lock = new ReentrantLock();
        this.condRead = lock.newCondition();
        this.condWrite = lock.newCondition();
        this.noReaders = 0;
        this.writer = false;
    }

    public int sum() {
        // Note the correct use of the lock variable throughout.
        // "lock". Acquire the lock only if it is absolutely necessary.

        // 1. as long as a writer is waiting or writing, sleep.

        // 2. increase the number of readers by 1.


        // 3. sum up
        int sum = super.sum();

        // 4. decrease the number of readers and, if necessary, wake up the writers.
        
        // 5. return the total
        return sum;
    }

    public void randomSwap() {
        // Note the correct use of the lock variable throughout.
        // "lock". Acquire the lock only if absolutely necessary.

        // 1. indicate that a writer is waiting.

        // 2. put to sleep as long as at least one reader is still active.

        // 3. swap elements
        super.randomSwap();

        // 4. indicate that no writer is waiting/writing anymore and
        // wake up readers if necessary.
    }
}

Code:
  public int sum() {
        // Note the correct use of the lock variable throughout.
        // "lock". Acquire the lock only if it is absolutely necessary.

        // 1. as long as a writer is waiting or writing, sleep.
        while(writer==true) {
            lock.lock();
        }
        // 2. increase the number of readers by 1.
        noReaders=noReaders+1;

        // 3. sum up
        int sum = super.sum();

        // 4. decrease the number of readers and, if necessary, wake up the writers.
        noReaders=noReaders-1;
        try {
            condWrite.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 5. return the total
        return sum;
    }

    public void randomSwap() {
        // Note the correct use of the lock variable throughout.
        // "lock". Acquire the lock only if absolutely necessary.

        // 1. show/indicate that a writer is waiting.
        
        //keine Ahnung was man damit meint, dass ich zeigen soll, dass ein writer wartet
        
        // 2. put to sleep as long as at least one reader is still active.
        while(writer) {
            lock.lock();
        }
        // 3. swap elements
        super.randomSwap();

        // 4. indicate that no writer is waiting/writing anymore and
        // wake up readers if necessary.
        if(writer==false) {
            try {
                condWrite.signal();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
 
Hast du das Verhalten denn mal getestet? Passiert das, was vorgegeben ist?

"writer" wird nie auf true gesetzt, es würde mich wundern, wenn alles klappen würde.


ps. Nicht verhaltensrelevant, nur code-Verbesserung
1.
Java:
while(writer==true) {
}
//ersetzen durch
while(writer) {
}


//und
if(writer==false) {
}
//ersetzen durch
if(!writer) {
}

2. benenne die Variable "writer" um nach "writing". "writer" klingt danach, als ob das damit bezeichnete Objekt etwas machen würde.

3. Deine Variablen sind nicht private sondern package protected, das ist im Normalfall nicht gut. Besser: Setze sie auf private und schreib getter und setter zum Auslesen und Setzen. So kannst du auch z.b. auch auf das Setzen reagieren.
 
Zuletzt bearbeitet:
Danke, aber bekomme das irgendwie trotzdem nicht hin. Ich habe noch eine Klasse Reader und Writer, die sind public und erben von Threads, muss ich die vielleicht hier im Code auch irgendwie verwenden?
 
Das war auch nicht die Lösung des Problems, nur ein kleiner Tip.
Aber wenigstens ist jetzt mal die Frage beantwortet, ob du es richtig gemacht hast: Nein 🙂

Nachdem ich deine Klassen nicht kenne (vor allem DataObject) kann ich nur raten, was das Ganze überhaupt bezwecken soll und wie die Vorgabe umzusetzen wäre.
Meine Annahme wäre, dass die Zugriffsmethoden von DataObject überschrieben werden müssen und geprüft werden muss, ob Änderung gerade zulässig ist oder nicht. Falls der Thread warten muss, dann ist das einzubauen. Das wäre so die übliche Vorgehensweise bei LockedDingsbums-Klassen.
 

Zurück
Oben