Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. package lift;
  2.  
  3. public class Monitor {
  4. private int here;
  5. private int next;
  6. private int[] waitEntry;
  7. private int[] waitExit;
  8. private int load;
  9. private boolean dirUp;
  10.  
  11. public synchronized void enterLift(int personLoc, int target){
  12. try {
  13. while(here != personLoc){ wait(); }
  14. } catch (InterruptedException e) {
  15. // TODO Auto-generated catch block
  16. e.printStackTrace();
  17. }
  18.  
  19. if(load <= 4) {
  20. load++;
  21. waitExit[target]++;
  22. waitEntry[personLoc]--;
  23. } else {
  24. waitEntry[personLoc]++;
  25. }
  26.  
  27. notifyAll();
  28. }
  29.  
  30. public synchronized void leaveLift(int target){
  31. try {
  32. while(here != target) { wait(); }
  33. } catch (InterruptedException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37.  
  38. load--;
  39. waitExit[here]--;
  40.  
  41. notifyAll();
  42. }
  43.  
  44. public synchronized void moveLift(){
  45. try {
  46. while((waitEntry[here] > 0 && load <= 4) || waitExit[here] > 0) { wait(); } // (waitQ > 0 && load < 4) eller liftQ > 0
  47. } catch (InterruptedException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51.  
  52. liftMovement();
  53.  
  54. notifyAll();
  55. }
  56.  
  57. private void liftMovement() {
  58. if(next > 6) {
  59. dirUp = false;
  60. } else if (next < 0){
  61. dirUp = true;
  62. }
  63.  
  64. if(dirUp){
  65. if(next <= -1) {
  66. next = 1;
  67. here = 0;
  68. } else {
  69. next++;
  70. here++;
  71. }
  72. } else {
  73. if(next >= 7){
  74. next = 5;
  75. here = 6;
  76. } else {
  77. next--;
  78. here--;
  79. }
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement