daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package lab4;
  6.  
  7. import java.util.concurrent.Semaphore;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. /**
  12.  *
  13.  * @author k3liang
  14.  */
  15. public class Philosphers implements Runnable {
  16.     private static final long startTime= System.currentTimeMillis();
  17.     public static final int MAX_ITERATIONS=3;
  18.     private State state;
  19.     private int  philID;
  20.     private static int nextID=1;
  21.     public Semaphore semaphore;
  22.     public static Semaphore semaphore2;
  23.  
  24.     public Philosphers Left;
  25.     public Philosphers Right;
  26.    
  27.     public Philosphers()
  28.     {
  29.     philID=nextID++;
  30.     semaphore= new Semaphore(0,true);
  31.     semaphore2= new Semaphore(1,true);
  32.  
  33.  
  34.     }
  35.    
  36. @Override
  37. public void run() {
  38.    System.out.println("STARTING #" + philID);
  39.    for (int i = 0; i < MAX_ITERATIONS; i++) {
  40.       state = State.THINKING;
  41.      
  42.      
  43.       displayState();
  44.       sleepAndDisplay();
  45.        try {
  46.                 getFork();
  47.             } catch (InterruptedException ex) {
  48.                 Logger.getLogger(Philosphers.class.getName()).log(Level.SEVERE, null, ex);
  49.        }
  50.    //   state = State.HUNGRY;
  51.       sleepAndDisplay();
  52.    //   state = State.EATING;
  53.       sleepAndDisplay();
  54.  
  55.       try{
  56.      putFork();
  57.                   } catch (InterruptedException ex) {
  58.                 Logger.getLogger(Philosphers.class.getName()).log(Level.SEVERE, null, ex);
  59.        }
  60.  
  61.    }
  62. }
  63.  
  64. private void sleepAndDisplay() {
  65.    try {
  66.       Thread.sleep(0, (int) (1000000 * Math.random()));
  67.    } catch (InterruptedException ex) {
  68.    }
  69.    displayState();
  70. }
  71.  
  72. private void displayState() {
  73.     long timeNow = System.currentTimeMillis() - startTime;
  74.     System.out.println("Philosoper #" + philID + ": "
  75.             + state + " time: " + timeNow + "ms");
  76.   }
  77.  
  78.  
  79.  
  80. private void getFork() throws InterruptedException
  81. {
  82.  
  83.    
  84. state = State.HUNGRY;
  85. while ( state == State.HUNGRY )
  86. {
  87.    semaphore.acquire();
  88.    if ( state == State.HUNGRY && Left.state != State.EATING && Right.state != State.EATING )
  89.             {
  90.                   state = State.EATING;
  91.                   semaphore2.release();
  92.              }
  93.                 semaphore.release();
  94.                 semaphore2.acquire();
  95.    }
  96. }
  97.  
  98.  
  99.  
  100.  
  101. private void putFork() throws InterruptedException
  102. {
  103.     semaphore.acquire();
  104. state= State.THINKING;
  105. if ( Left.state == State.HUNGRY)
  106.         semaphore2.release();
  107. if ( Right.state == State.HUNGRY)
  108.        semaphore2.release();
  109.     semaphore.release();
  110.  
  111.    
  112. }
  113.  
  114.  
  115.  
  116. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top