[Cst-1b] CST1-b Question: Java 98/4/3

Hugh Nimmo-Smith hrfn2@cam.ac.uk
Wed, 3 May 2000 09:29:21 +0100


My solution was something like this... not very different but possibly
safer:

class SyncBuffer {

  int value; // value in buffer

  boolean valid; // is there a value in teh buffer

  TicketMachine m = new TicketMachine();
  Scheduler s = new Scheduler();

  void put(int v)
  {
    boolean put = false; // have we put are value into buffer yet??

    while (!put)
    {
      int number = m.turn();
      s.queue(number);
      // we now have lock on object

      // if there isn't a value in buffer then put our value into buffer
      if (!valid) {

        // put value in to buffer
        value = v;

        // remember we have put value
        put = true;

        // mark buffer as holding a value
        valid = true;
      }

      s.next(); // notify other waiting threads
    }
  }

  int get()
  {
    int result;
    boolean got_result = false; // have we got a value from buffer yet?

    while (!got_result)
    {
      int number = m.term();
      s.queue(number);
      // we now have lock on object

      // if there is a value in the buffer then get it...
      if (valid) {

        // get the result
        result = value;

        // set the buffer to not contain a value
        valid = false;

        // remeber that we have got a value
        got_result = true;
      }
    }
  }
}
> -----Original Message-----
> From: cst-1b-admin@srcf.ucam.org [mailto:cst-1b-admin@srcf.ucam.org]On
> Behalf Of M.Y.W.Y.B.
> Sent: 02 May 2000 22:03
> To: cst-1b@srcf.ucam.org
> Subject: [Cst-1b] CST1-b Question: Java 98/4/3
>
>
> CST 98/4/3
> http://www.cl.cam.ac.uk/tripos/y1998p4q3.pdf
>
> Hi!
>
> Has anyone solved the last part of the question in 98/4/3?
> "Show how a synchronised buffer holding a single value could be
> implemented
> using this new scheme".
>
> I assume that you are not allowed to use "synchronized", "wait()",
> "notify()" etc but only TicketMachine and Scheduler methods. But how?
>
> Thanks
>
> Moritz
>
>
> _______________________________________________
> CST-1B mailing list
> CST-1B@srcf.ucam.org
> http://www.srcf.ucam.org/mailman/listinfo/cst-1b
>