Java Unisex bathroom

I have to use Java semaphores to solve this problem, but I don't know how. I can't find any relevant Java data What's going on:

There are all kinds of clues: men and women Both want to use bathroom_ The same resource as size Five rules:

>After signaling that a resource needs to be used, each thread should wait until it can use it. > When multiple bathoom_ Prevent this from happening when the size thread uses resources at the same time. > Prevent women and men from using the bathroom at the same time. > Threads should use resources simultaneously If there are many threads of one type, bathroom_ The size thread should use resources. > Prevent hunger

result

For:

1 female, 1 person, 5 female, 5 persons

Failed:

5women1men,5men1women,2men2women,5men5women.

I've been trying to make it successful since Monday, and now I've run out of ideas

code

Therefore, my task is to write a bathroom that implements the bathrominterface Java class:

public interface BathroomInterface {

    public static final int BATHROOM_SIZE = 3; //3 is just example
    void manEnter();
    void manExit();
    void womanEnter();
    void womanExit();
}

There are many male and female threads in the system that work like this:

for(int i = 0; i < n; i++) {
  bathroom.manEnter();
  //uses bathroom random amount of time
  bathroom.manExit();
}

for(int i = 0; i < m; i++) {
  bathroom.womanEnter();
  //uses bathroom random amount of time
  bathroom.womanExit();
}

I also have bathroom For the scheme of Java class, I must extend:

import java.util.concurrent.Semaphore;

public class Bathroom implements BathroomInterface {


    private Semaphore mutex = new Semaphore(1,true);

    public void womanEnter() {
        mutex.acquireUninterruptibly();
    }

    public void womanExit() {
        mutex.release();
    }

    public void manEnter() {
        mutex.acquireUninterruptibly();        
    }

    public void manExit() {
        mutex.release();
    }
}

This is what I have done so far:

import java.util.concurrent.Semaphore;

public class Bathroom implements BathroomInterface {
    int manW=0,manU=0,womanW=0,womanU=0; //*U-using,*W-waiting
    private Semaphore mutex = new Semaphore(1,false);

    public void womanEnter() {
        womanW++;
        StateChange();
    }

    public void womanExit() {
        womanU--;
        mutex.release();
        StateChange();
    }

    public void manEnter(){
        manW++;
        StateChange();
    }

    public void manExit() {
        manU--;
        mutex.release();
        StateChange();
    }

    void StateChange() {
        if(womanU==0 && manU==0) {
            if(manW>womanW) {
                while(manW>0 && manU<BATHROOM_SIZE) {
                    manW--;
                    manU++;
                    mutex.acquireUninterruptibly();
                }
            }
            else {
                while(womanW>0 && womanU<BATHROOM_SIZE) {
                    womanW--;
                    womanU++;
                    mutex.acquireUninterruptibly();
                }
            }
        }
        if(womanU==0 && manU<BATHROOM_SIZE) {
            while(manW>0 && manU<BATHROOM_SIZE) {
                manW--;
                manU++;
                mutex.acquireUninterruptibly();
            }
        }
        if(manU==0 && womanU<BATHROOM_SIZE) {
            while(womanW>0 && womanU<BATHROOM_SIZE) {
                womanW--;
                womanU++;
                mutex.acquireUninterruptibly();
            }
        }
    }
}

Solution

In fact, this exercise is done using a monitor, not a semaphore Most of what you are doing is very good. You missed the condition So, in your bathroom class, state:

A lock:

private Lock lock = new reentrantlock();

2 conditions or queues attached to the lock:

private Condition womenWaitingQueue = lock.newCondition();
private Condition menWaitingQueue = lock.newCondition();

2 counters know how many people are waiting and 2 counters know how many people are using:

private int womenWaitingN = 0;
private int menWaitingN = 0;
private int womenUsingN = 0;
private int menUsingN = 0;

And, of course, the number of resources:

private final int BATHROOM_CAPACITY = 5;
private int free_resources = BATHROOM_CAPACITY;

All four features are here, but the homework tab has been deleted

The most important thing here is to prevent hunger. If there are women waiting and vice versa, no man is allowed to enter the bathroom

Therefore, the condition is that if a man wants to enter the bathroom, it must check whether there is at least one free point in the bathroom (use free resources) and if there are women in the bathroom (use womenusingn) If either of these two conditions is not met, the person must wait (using menwaitingqueue):

menWaitingQueue.await();

When a man leaves the bathroom, he must check whether there are women waiting (women waiting n). If so, they will be notified:

womanWaitingQueue.signal();

Because of the menusingn counter, women send signals until there are no men in the bathroom If there are no women waiting, you can signal men to enter the bathroom This prevents hunger because the opposite sex is given priority (if waiting)

Finally, each function must lock / unlock at the beginning / end of each entry / exit function

lock.lock();
lock.unlock();

I think with this new information, you will be able to complete these functions yourself Good luck!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>