0

今天是个好日子!

我需要帮助来解决我的 java 多线程问题,这是一个中性舒适室的问题;

  1. 打造男女皆宜的舒适客房
  2. 如果男性在场,男性可以进入(反之亦然)
  3. 如果男性使用女性不能进入(反之亦然)
  4. 公平使用
  5. 没有饥饿

到目前为止,我想出了一些接近的东西,但我在公平使用方面遇到了问题,男性使用女性无法进入和饥饿。我需要这方面的帮助。!

import java.util.Random;
import java.util.concurrent.Semaphore;

public class Try2 extends Thread {

    public static Semaphore USERS = new Semaphore(0);
    public static Semaphore PUBLIC_CR = new Semaphore(0);
    public static Semaphore USAGE = new Semaphore(1);

    public static final int LIST = 3;
    public static int a = 0;
    public static int c = 0;

    public static int VACANCY = LIST;

    class MEN extends Thread {

        int iD;
        boolean notUsed = true;

        public MEN(int i) {
            iD = i;
        }

        public void run() {
            while (notUsed) {
                try {
                    USAGE.acquire();
                    if (VACANCY > 0) {
                        System.out.println("MALE " + this.iD + " Is USING");
                        VACANCY--;
                        USERS.release();
                        USAGE.release();
                        try {
                            PUBLIC_CR.acquire();
                            notUsed = false;
                            this.USING_THE_CR();
                        } catch (InterruptedException ex) {
                        }
                    } else {
                        USAGE.release();
                        notUsed = false;
                    }
                } catch (InterruptedException ex) {
                }
            }
        }

        public void USING_THE_CR() {
            System.out.println("MALE " + this.iD + " is done USING the Comfort Room");
            try {
                sleep(1000);
            } catch (InterruptedException ex) {
            }
        }

    }

    class WOMEN extends Thread {

        int iD;
        boolean notUsed = true;

        public WOMEN(int i) {
            iD = i;
        }

        public void run() {
            while (notUsed) {
                try {
                    USAGE.acquire();
                    if (VACANCY > 0) {
                        System.out.println("FEMALE " + this.iD + " Is USING");
                        VACANCY--;
                        USERS.release();
                        USAGE.release();
                        try {
                            PUBLIC_CR.acquire();
                            notUsed = false;
                            this.USING_THE_CR();
                        } catch (InterruptedException ex) {
                        }
                    } else {

                        USAGE.release();
                        notUsed = false;
                    }
                } catch (InterruptedException ex) {
                }
            }
        }

        public void USING_THE_CR() {
            System.out.println("FEMALE " + this.iD + " is DONE USING the Comfort Room");
            try {
                sleep(1000);
            } catch (InterruptedException ex) {
            }
        }

    }

    class BATHROOM extends Thread {

        public BATHROOM() {
        }

        public void run() {
            while (true) {  // runs in an infinite loop
                try {
                    USERS.acquire();
                    USAGE.release();
                    VACANCY++;
                    PUBLIC_CR.release();
                    USAGE.release();
                    this.USING();
                } catch (InterruptedException ex) {
                }
            }
        }

        public void USING() {
            System.out.println("The Comfort Room is occupied");
            try {
                sleep(1000);
            } catch (InterruptedException ex) {
            }
        }
    }

    public static void main(String args[]) {

        Try2 BR = new Try2();
        BR.start();  // Let the simulation begin
    }

    public void run() {
        BATHROOM b = new BATHROOM();  //WORKING THREADS ON BATHROOM
        b.start();

        /* THIS WILL GENERATE THE USERS*/
        for (int i = 1; i < 16; i++) {

            Random random = new Random();
            int temp = random.nextInt(2);
            if (temp == 0) {
                a++;

                MEN m = new MEN(a);
                m.start();

            } else {
                c++;
                WOMEN w = new WOMEN(c);
                w.start();

            }

            try {
                sleep(2000);
            } catch (InterruptedException ex) {
            };
        }
    }
}
4

0 回答 0