java Semaphore北南阙 [英] java Semaphore north south que

查看:101
本文介绍了java Semaphore北南阙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个孩子在等待在操场上使用环形交叉路口 - 一个是从北面朝下,一个是南面。儿童只能从任一队列的前面进入环形交叉口,并且只有在有空位的情况下才可进入(每次只有一个孩子可以使用每个分段)。一旦进入环形交叉路口,他们会随机使用它,然后随意离开东部或西部。然后他们在其他地方随机游玩,之后,随机重新进入北/南队列,等等无限期。环形交叉口顺时针旋转,排队的孩子将始终使用第一个空间......
使用java信号量编写程序,通过一组代表孩子的进程来同步对共享环形交叉对象的访问。

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.

这是我到目前为止所做的,不知道接下来该做什么。我在Main class做什么?

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

推荐答案

您只是模仿儿童,而不是实际的环形交叉路口。我怀疑每个孩子都需要自己的线程,除非这是强制性的。

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.

这篇关于java Semaphore北南阙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆