多线程和信号量来模拟电梯

时间:2016-10-27 01:38:00

标签: java multithreading semaphore

所以,我有一个项目,我使用线程和信号量模拟电梯。所以,我正在使用arrayList存储电梯将要到达的楼层。当时有49人,只有7人可以入住。我不能使用sync或sleep或availablePermits作为逻辑。所以我所坚持的是让电梯发出信号,让线程在适当的楼层退出电梯。我被告知我应该使用一个数组存储信号量,当电梯到达他们的楼层时,他们会发出信号退出的信号。我不完全理解我如何编写代码。我理解如何在数组中存储值,但是我将如何使用它来触发乘客的部分是我没有得到的。并且线程本身将打印它离开

模拟器

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.LinkedHashSet;
    import java.util.Set;

    public class Simulator 
    {
        //Creates ArrayList

   static ArrayList<Integer> visit_floors = new ArrayList<Integer();
        static ArrayList<Person> passengers = new ArrayList<>();

        static volatile int currentFloor;
        public static void main(String[] args) throws Exception
        {

            //Creates elevator and PassengerThread
            Thread elevator = new Thread(new Elevator());
            Threads passenger = new Threads();
            elevator.start();
            passenger.startThreads();
            Collections.sort(visit_floors);
            clearDupl();
        //  System.out.println(visit_floors);
            //System.out.println(Elevator.Passenger);
        }


        public static void clearDupl()
        {
            visit_floors = new ArrayList<Integer>(new LinkedHashSet<Integer>(visit_floors));
        }

    }

线程

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

public class Threads 
{
private int numThreads = 49;
public Thread passengerT[] = new Thread[numThreads];
private int num;
    public static int RandomFloor()
    {
        Random random = new Random();
        int floor = random.nextInt(6) + 2;
        return floor;
    }
     public void startThreads() throws Exception
     {
        //Creates ArrayList
        //ArrayList<Integer> visit_floors = new ArrayList  <Integer>(); 
        //Creates Passenger Threads
        for(int i=1;i<numThreads;++i)
        {
            passengerT[i] = new Thread(new Person(i,RandomFloor()));
            passengerT[i].start();
            try {
                passengerT[i].join();

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}
}

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

    public class Person implements Runnable {

        //Defines person
        int passenger;
        int floor;
        private int numThreads = 49;
        Elevator e = new Elevator();
        //Defines the sem with 7 keys to give
        static Semaphore sem = new Semaphore(passengerSem());

        public Person(){};

        public Person(int person,int floor)
        {
            this.passenger=person;
            this.floor=floor;
            Simulator.visit_floors.add(floor);
        }
        public void run()
        {

            System.out.println("Person "+passenger+" enters the elevator to go to floor "+floor);

            try
            {

            //Adds permit to elevator
            //wait for key
            Elevator.signalElevator();
            sem.acquire();

            //Give access to the next 7 threads into elevator

            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        public static int passengerSem()
        {
            return 6;
        }

    }

电梯

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

    public class Person implements Runnable {

        //Defines person
        int passenger;
        int floor;
        private int numThreads = 49;
        Elevator e = new Elevator();
        //Defines the sem with 7 keys to give
        static Semaphore sem = new Semaphore(passengerSem());

        public Person(){};

        public Person(int person,int floor)
        {
            this.passenger=person;
            this.floor=floor;
            Simulator.visit_floors.add(floor);
        }
        public void run()
        {

            System.out.println("Person "+passenger+" enters the elevator to go to floor "+floor);

            try
            {

            //Adds permit to elevator
            //wait for key
            Elevator.signalElevator();
            sem.acquire();

            //Give access to the next 7 threads into elevator

            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        public static int passengerSem()
        {
            return 6;
        }

    }

示例输出

Elevator door opens at floor 1
Person 0 enters elevator to go to floor 5
Person 1 enters elevator to go to floor 2
Person 2 enters elevator to go to floor 8
Person 3 enters elevator to go to floor 4
Person 4 enters elevator to go to floor 6
Person 5 enters elevator to go to floor 7
Person 6 enters elevator to go to floor 2
Elevator door closes
Elevator door opens at floor 2
Person 1 leaves elevator  
Person 6 leaves elevator  
Elevator door closes
Elevator door opens at floor 4
Person 3 leaves elevator  
Elevator door closes
Elevator door opens at floor 5
Person 0 leaves elevator  
Elevator door closes
Elevator door opens at floor 6
Person 4 leaves elevator  
Elevator door closes
Elevator door opens at floor 7
Person 5 leaves elevator  
Elevator door closes
Elevator door opens at floor 8
Person 2 leaves elevator  
Elevator door closes
Elevator door opens at floor 1
…

1 个答案:

答案 0 :(得分:0)

如果没有为您完成作业,可以考虑将所需的信号量放在n列表中,其中n是楼层数。当该人告诉电梯他/她想要去楼i时,电梯将他/她的信号量放入i列表中。然后电梯可以发信号通知该列表中的所有信号量,让在该楼层下车的人离开。