java 信号量 南北 que

发布于 2024-08-15 03:30:59 字数 1739 浏览 6 评论 0原文

操场上有两队孩子在等待使用环岛——一队面向北,一队面向南。儿童只能从任一队列的前面进入环岛,并且只有在有可用空间的情况下才可以进入(每个路段一次只有一个儿童可以使用)。一旦到达环岛,他们会随机使用一段时间,然后随机离开,无论是向东还是向西。然后他们在其他地方随机玩一段时间,然后随机重新进入北/南队列,如此循环往复。环岛顺时针旋转,排队的孩子总是会使用第一个出现的空间…… 使用 java 信号量编写一个程序,通过代表子进程的一组进程来同步对共享环岛对象的访问。

这是我到目前为止所做的,不知道下一步该做什么。我在主课上做什么?

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();
    }
}

我到了这里,现在我迷路了!请协助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不乱于心 2024-08-22 03:30:59

您只模拟了孩子们,而不是实际的环岛。我怀疑每个孩子都需要自己的线程,除非这是强制性的。

似乎更有用的方法是创建三个线程,一个用于每个队列,一个用于环岛。环形交叉口是工作线程,子队列是生产者线程。您的环岛线程将有一个儿童循环缓冲区,每个孩子都有一个在进入环岛时随机决定的“玩耍时间”。该线程会定期检查每个孩子的“玩时间”,当其中任何一个孩子过期时,它会将他们随机弹出到北队列或南队列中,并发出一个信号量,表明空间已开放。

两个队列线程将各自等待信号量,每当信号量上升时,第一个获取信号量的线程就会将其子线程放入回旋结构中,并随机选择“播放时间”。

或者,您可以让迂回线程将人员随机弹出到东部和西部游乐场,并让排队线程负责清空他们。您需要确保每个共享集合(循环缓冲区和每个队列线程中的实际子级列表)在同步方面得到正确处理。您只需要两个类,即环形交叉口线程和队列线程,但队列线程有两个实例,一个用于北,一个用于南。

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.

偷得浮生 2024-08-22 03:30:59

信号量在哪里?其余的代码在哪里?如果这就是你所拥有的一切,那么我来帮助你还不够,感觉就像我正在做所有的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文