Question: Need some help in First Class add and remove method and implement with wait(),notifyall() multithread. Thanks a lot. CircularArrayLongFifo.class public class CircularArrayLongFifo implements LongFifo {
Need some help in First Class "add" and "remove" method and implement with wait(),notifyall() multithread. Thanks a lot.
CircularArrayLongFifo.class
public class CircularArrayLongFifo implements LongFifo { // do not change any of these fields: private final long[] slots; private int head; private int tail; private int count; private final Object lockObject;
// this constructor is correct as written - do not change public CircularArrayLongFifo(int fixedCapacity, Object proposedLockObject) {
lockObject = proposedLockObject != null ? proposedLockObject : new Object();
slots = new long[fixedCapacity]; head = 0; tail = 0; count = 0; }
// this constructor is correct as written - do not change public CircularArrayLongFifo(int fixedCapacity) { this(fixedCapacity, null); }
// this method is correct as written - do not change @Override public int getCount() { synchronized ( lockObject ) { return count; } }
@Override public boolean isEmpty() { synchronized ( lockObject ) { return count == 0; } }
@Override public boolean isFull() { synchronized ( lockObject ) { return count == slots.length; } }
@Override public void clear() { synchronized ( lockObject ) { // No need - just keep the old junk (harmless): // Arrays.fill(slots, 0); head = 0; tail = 0; count = 0; } }
@Override public int getCapacity() { return slots.length; }
/** * Add the specified item to the fifo. * If currently full, the calling thread waits until there is space and * then adds the item. * If this method doesn't throw InterruptedException, then the item was * successfully added. */
@Override public void add(long value) throws InterruptedException { }
/** * Removes and returns the next item. * If currently empty, the calling thread waits until another thread adds * an item. * If this method doesn't throw InterruptedException, then the item was * successfully removed. */
@Override public long remove() throws InterruptedException { return 0L; }
// this method is correct as written - do not change @Override public Object getLockObject() { return lockObject; } }
LongFifo.class
public interface LongFifo { /** Returns the number if items currently in the FIFO. */ int getCount();
/** Returns true if {@link #getCount()} == 0. */ boolean isEmpty();
/** Returns true if {@link #getCount()} == {@link #getCapacity()}. */ boolean isFull();
/** Removes any and all items in the FIFO leaving it in an empty state. */ void clear();
/** * Returns the maximum number of items which can be stored in this FIFO. * This value never changes. */ int getCapacity();
/** * Add the specified item to the fifo. * If currently full, the calling thread waits until there is space and * then adds the item. * If this method doesn't throw InterruptedException, then the item was * successfully added. */ void add(long value) throws InterruptedException;
/** * Removes and returns the next item. * If currently empty, the calling thread waits until another thread adds * an item. * If this method doesn't throw InterruptedException, then the item was * successfully removed. */ long remove() throws InterruptedException;
/** * Returns a reference to use for synchronized blocks which need to * call multiple methods without other threads being able to get in. * Never returns null. */ Object getLockObject(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
