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

Nathan Dimmock ned21@cam.ac.uk
Wed, 03 May 2000 00:04:00 +0100


"M.Y.W.Y.B." wrote:
> 
> --On Mittwoch, 3. Mai 2000, 22:46 +0100 "Martin Harper" <mcnh2@cam.ac.uk>
> wrote:
> 
> > Surely you do exactly the same as synchronization, but instead of writing
> -
> >
> >         synchronized {
> >             // protected code
> >         }
> >
> > you write -
> >         int tick = m.turn();
> >         s.queue(tick);
> >             // protected code
> >         s.next()
> > (where m & s are a machine and a scheduler for the given object...)
> 
> I think it's not as simple as that. Don't forget it's a *bounded* buffer
> (bound=1 element). For that we need two methods: put(int value) and get().

How about:

Implement two more methods on the buffer which return booleans -
hasValue() and isEmpty().  Then at the beginning of your protected code
do the appropriate test, and if you get a false result, take another
ticket and wait a bit longer (and keep doing this until your test
successfully).

So for a producer, on a buffer b.

boolean done = false;
int tick;
while (!done)
{
	tick = m.turn();
	s.queue(tick);
	if (b.isEmpty())
	{ 
		b.put(val);
		done = true;
	}
	s.next();
}

Just a thought... 
-- 
Nathan 
Jesus College, Cambridge, CB5 8BL
http://www-jcsu.jesus.cam.ac.uk/~ned21/