Facebook
From Ample Owl, 6 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 234
  1. import java.util.concurrent.Semaphore;
  2.  
  3. public class Bufor {
  4.     private int size = 40;
  5.     private String[] data = new String[size];
  6.     private int in;
  7.     private int out;
  8.     private int counter;
  9.     private int endCount;
  10.     private boolean end = false;
  11.     static Semaphore elementy = new Semaphore(0);
  12.     static Semaphore miejsca = new Semaphore(400);
  13.  
  14.  
  15.     Bufor(int _end, Semaphore e, Semaphore m) {
  16.         this.in = 0;
  17.         this.out = 0;
  18.         this.counter = 0;
  19.         this.endCount = _end;
  20.         this.elementy = e;
  21.         this.miejsca = m;
  22.  
  23.     }
  24.  
  25.     public String get() {
  26.         elementy.acquireUninterruptibly();
  27.         if(counter >= endCount) {
  28.             this.end = true;
  29.             return "-1";
  30.         }
  31.         String temp = this.data[out];
  32.         this.out = (this.out + 1) % size;
  33.         miejsca.release();
  34.         counter++;
  35.  
  36.         if(counter >= endCount) {
  37.             this.end = true;
  38.         }
  39.         return temp;
  40.  
  41.     }
  42.  
  43.     public void add(String _data) {
  44.         miejsca.acquireUninterruptibly();
  45.         data[in] = _data;
  46.         in = (in + 1) % size;
  47.         elementy.release();
  48.     }
  49.  
  50.     public synchronized boolean check(){
  51.         if(this.end){
  52.             return true;
  53.         }
  54.         return false;
  55.     }
  56. }