Advertisement
Guest User

PostOfficeQueue

a guest
Jul 24th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. public class PostOfficeQueue {
  2.     private int tot;
  3.     int l[];
  4.    
  5.     public PostOfficeQueue(int n){
  6.         tot=n;
  7.         l=new int[n];
  8.         for(int temp:l){
  9.             temp=0;
  10.         }
  11.     }
  12.    
  13.     public synchronized void deskStart(int i){
  14.         l[i]=1;
  15.         tot--;
  16.     }
  17.    
  18.     public synchronized void deskFinish(int i){
  19.         l[i]=0;
  20.         tot--;
  21.         notifyAll();
  22.     }
  23.    
  24.     public synchronized int getFreeDesk(){
  25.         while(tot==0){
  26.             try{
  27.                 wait();
  28.             }catch(InterruptedException e){
  29.                 return -1;
  30.             }
  31.         }
  32.         int j=-1;
  33.         for(Integer temp:l){
  34.             j++;
  35.             if(temp==0)return j;
  36.         }
  37.         return j;
  38.     }
  39.    
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement