[Cst-1b] CST1-b Question: Java 98/4/3
Hugh Nimmo-Smith
hrfn2@cam.ac.uk
Wed, 3 May 2000 12:51:12 +0100
I disagree because Scheduler.queue(int number) should block
eg.
class Scheduler {
private int number = 0;
void queue(int n)
{
// if it isnt time for n then block
while (number < n) wait();
}
synchronized void next()
{
number++;
notifyAll(); // notify other waiting threads
}
}
That was my interpretation of the question
Hugh
> -----Original Message-----
> From: William R Sowerbutts [mailto:will@sowerbutts.com]
> Sent: 03 May 2000 11:13
> To: Hugh Nimmo-Smith
> Cc: M.Y.W.Y.B.; cst-1b@srcf.ucam.org
> Subject: Re: [Cst-1b] CST1-b Question: Java 98/4/3
>
>
> I would argue that this IS very different. If one thread calls
> put() when the
> buffer already contains a value, then this thread will eat up any
> available
> CPU runtime until the buffer become empty. Likewise, calling
> get() on an empty
> buffer will eat CPU runtime quite happily until the buffer is full.
>
> Martin's solution was very much better.
>
>
>
> On Wed, May 03, 2000 at 09:29:21AM +0100, Hugh Nimmo-Smith wrote:
> >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
> >>
> >
> >
> >
> >_______________________________________________
> >CST-1B mailing list
> >CST-1B@srcf.ucam.org
> >http://www.srcf.ucam.org/mailman/listinfo/cst-1b
>
>
> _________________________________________________________________________
> William R Sowerbutts (BtG) will@sowerbutts.com
> Coder / Guru / Nrrrd http://sowerbutts.com
> main(){char*s=">#=0> ^#X@#@^7=";int c=0,m;for(;c<15;c++)for
> (m=-1;m<7;putchar(m++/6&c%3/2?10:s[c]-31&1<<m?42:32));}
>
>