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:
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();
}
}
}