Bounded Buffer

 

class Bounded_Buffer {

        protected int size_;

        protected static final int MAX = 10;

        // private Implementation details

 

        public synchronized void put(Object item){

                while (size_ == MAX){

                        try {

                                wait();

                        }

                        catch (InterruptedException ex) {}

                }

                // store item

                size_++;

                if ( size_ == 1)

                        notifyAll(); // no longer empty

        }

 

        public synchronized Object get(){

                while ( size_ == 0 ){

                        try {

                                wait();

                        }

                        catch (InterruptedException ex) {}

                }

                // remove item

                size_--;

                if (size_ == MAX-1)

                        notifyAll(); // no longer full

                return item;

        }

}


put2() – naiver Ansatz

 

class D2a_Bounded_Buffer extends Bounded_Buffer {

        public synchronized void put2(Object o1, Object o2){

                put(o1);

                put(o2);

        }

}

 

 

put2() – 2. Versuch

 

class D2b_Bounded_Buffer extends Bounded_Buffer {

        public synchronized void put2(Object o1, Object o2){

                while (size_ == MAX-1){

                        try {

                                wait();

                        }

                        catch (InterruptedException ex) {}

                }

                // store items

                size_ = size_ + 2;

                if (size_ == 2)

                        notifyAll(); // can release two get()'s if waiting

        }

}

 

 

put2() – neues get()

 

public synchronized Object get(){

        Object item = super.get();

        notifyAll();

        return item;

}


gget()

 

class D4_Bounded_Buffer extends Bounded_Buffer {

        protected static final int GET = 0;

        protected static final int PUT = 1;

        protected int last_op;

 

        D4_Bounded_Buffer() { last_op = -1; }

 

        public synchronized void put(Object item) {

                super.put(item);

                last_op = PUT;  // set flag

        }

 

        public synchronized Object get() {

                Object temp = super.get();

                last_op = GET;

                return temp;

        }

 

        public synchronized Object gget() {

                while ( last_op != GET ){

                        try {

                                wait();

                        }

                        catch (InterruptedException ex) {}

                }

                return get();

        }

}


gget() – 2. Versuch

 

public synchronized Object ggetV2() {

        while (last_op != GET || size_ == 0){

                try {

                       wait();

                }

                catch (InterruptedException ex) {}

        }

        return get();

}

 

 

gget() – neues put()

 

public synchronized void put(Object item){

        super.put(item);

        notifyAll();

}


Lockable Bounded Buffer

 

Voraussetzung: get und put verwenden notifyAll()

                         bedingungslos

 

class D5_Bounded_Buffer extends Bounded_Buffer {

        protected boolean locked_;

       

        D5_Bounded_Buffer() { locked_ = false; }

 

        public synchronized void lock() { //... }

        public synchronized void unlock(){ //... }

 

public synchronized void put(Object item) {

                while ( locked_ || size_ == MAX ){

                        try {

                                wait();

                        }

                        catch (InterruptedException ex) {}

                }

                super.put(item);

        }

 

        public synchronized Object get() {

                while ( locked_ || size_ == 0 ){

                        try {

                                wait();

                        }

                        catch (InterruptedException ex) {}

                }

                return super.get();

        }

}


So geht´s nicht:

 

public synchronized void put(Object item) {

                while ( locked_ ){

                        try {

                                wait();

                        }

                        catch (InterruptedException ex) {}

                }

                super.put(item);

}