views:

229

answers:

3

There are two queues of children waiting to use a roundabout in a playground – one is facing it from the north, one from the south. Children may only enter the roundabout from the front of either queue and may only enter if there is a space available (only one child may use each segment at a time). Once on the roundabout they use it for a random period of time, then leave, either to the east or west, at random. They then play elsewhere for a random period and, after that, re-enter a north/south queue at random, and so on ad infinitum. The roundabout rotates clockwise and a queuing child will always use the first space that comes along… Write a program using java semaphores to synchronise access to the shared roundabout object by set of processes that represent the children.

Here is what I have done so far, and don't know what to do next. What do I do in Main class?

import java.util.Random;

public class Child extends Thread {
    private Random random;
    private int  which;
    private int  number;

    public Child(int number) {
     this.number = number;
     random = new Random();
     this.which = random.nextInt(2);
    }

    public void run() {
    //start point?
    }

    public int getNumber() {
     return number;
    }

    private void checkQuePosition() {
     if (atFront()) 
      tryToGetOn();
     else 
      checkQuePosition();
    }

    //returns true if at front of que, else false
    private boolean atFront() {
     int position;
     if (which == 0) 
      position = Playground.north.que.search(this);
     else
      position = Playground.south.que.search(this);
     return position == 1;
    }

    private void tryToGetOn() {
     Playground.roundabout.semaphore.acquire();
     //get into the roundabout somehow
    }

    //releases semaphore, sleeps for a random period then calls joinQue(random 0 or 1)
    public void getOff() {
     Playground.roundabout.semaphore.release();
     Thread.sleep(random.nextLong());
     joinQue(random.nextInt(2));
    }

    private void joinQue(int w) {
     this.which = w;
     if (w == 0) {
      //join north que
     }
     else
      ;//join south que
     checkQuePosition();
    }
}

I got here and now I am lost! Please assist

A: 

You've only modeled the children, not the actual roundabout. I doubt each child needs its own thread, unless that's been mandated.

What seems a more useful approach is to make three threads, one for each queue, and one for the roundabout. The roundabout is the worker thread and the child queues are the producer threads. Your roundabout thread would have a circular buffer of children, each with a 'time to play' decided randomly when they enter the roundabout. The thread would periodically check the 'time to play' of each child and when any of them expire it would eject them randomly into the north or south queue and raise a semaphore that a space is open.

The two queue threads would each wait on the semaphore and whenever it went up, the first one to acquire it would put its child into the roundabout structure with a randomly chosen 'time to play'.

Alternatively you could have the roundabout thread eject people into the east and west playgrounds at random and have the queuing threads responsible for emptying them. You need to ensure that each shared collection (the circular buffer and the actual list of children in each of the queue threads) is properly handled in terms of synchronization. You will only need two classes, the roundabout thread and the queue thread, but there will be two instances of the queue thread, one for north and one for south.

Jherico
More than likely, they want an application that will simply run forever. The app as you've described it has no output, so the design of the program is the point. I expect that the grading for something like this will be executing it for a few hours to ensure that it a) doens't lose any children along the way and b) doesn't enter a non-viable state like a deadlock condition.
Jherico
why don't you take a shot at it and if you encounter a problem you can't figure out, come back and ask a more specific question.
Jherico
roundabout class is the problem.....
Mult
A: 

wow... this looks incredibly like an assessed homework I've been set... fancy that!

Jane Lee
A: 

Where are the semaphores? and where's the rest of the code? if this is all you have, it's not enough for me to help you, it'd feel like i'm doing the whole thing.

your roundabout doesn't seem to exist at all, unless that's how you intend the homework to be. but i'd add some semaphores and a mutex to ensure they don't get on roundabout when it's ocupied by somone else.
cool, i'll look into it later tonight. but in the mean time ask other coders on the board, they're pretty helpfull, make sure your question is visible to most ppl. will get back to you soon.
Thanks a lot greenman for all ur help... i really appreciate it... please keep in touch after you have got to some conclusion....i'll be waiting... i am still in doubt whether its right or wrong... but thanks again...
Mult
Hey Greenman... Did u look for what sort of output would come out? or what have u got?
Mult
i am not able to make the roundabout class...any help there???
Mult