Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lift;
- public class Monitor {
- private int here;
- private int next;
- private int[] waitEntry;
- private int[] waitExit;
- private int load;
- private boolean dirUp;
- public synchronized void enterLift(int personLoc, int target){
- try {
- while(here != personLoc){ wait(); }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(load <= 4) {
- load++;
- waitExit[target]++;
- waitEntry[personLoc]--;
- } else {
- waitEntry[personLoc]++;
- }
- notifyAll();
- }
- public synchronized void leaveLift(int target){
- try {
- while(here != target) { wait(); }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- load--;
- waitExit[here]--;
- notifyAll();
- }
- public synchronized void moveLift(){
- try {
- while((waitEntry[here] > 0 && load <= 4) || waitExit[here] > 0) { wait(); } // (waitQ > 0 && load < 4) eller liftQ > 0
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- liftMovement();
- notifyAll();
- }
- private void liftMovement() {
- if(next > 6) {
- dirUp = false;
- } else if (next < 0){
- dirUp = true;
- }
- if(dirUp){
- if(next <= -1) {
- next = 1;
- here = 0;
- } else {
- next++;
- here++;
- }
- } else {
- if(next >= 7){
- next = 5;
- here = 6;
- } else {
- next--;
- here--;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement