Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import java.util.LinkedList;
  2.  
  3. public class BlockingQueue<T> {
  4.  
  5.     private LinkedList<T> lista = new LinkedList<T>();
  6.     private int maxNumber;
  7.    
  8.     public BlockingQueue()
  9.     {
  10.         maxNumber = -1;
  11.     }
  12.    
  13.     public BlockingQueue(int max)
  14.     {
  15.         maxNumber = max;
  16.     }
  17.    
  18.     public synchronized void offer(T e) throws InterruptedException
  19.     {
  20.         while(size() > maxNumber && maxNumber != -1)
  21.         {
  22.             wait();
  23.         }
  24.        
  25.         lista.add(e);
  26.         System.out.println("Meteu " + e.toString());
  27.         notifyAll();
  28.     }
  29.    
  30.     public synchronized T poll() throws InterruptedException
  31.     {
  32.         while(size() == 0)
  33.         {
  34.             wait();
  35.         }
  36.        
  37.         T e = lista.pop();
  38.         System.out.println("Retirou " + e.toString());
  39.         notifyAll();
  40.         return e;
  41.     }
  42.    
  43.     public synchronized int size()
  44.     {
  45.         return lista.size();
  46.     }
  47.    
  48.     public synchronized void clear()
  49.     {
  50.         lista.clear();
  51.         notifyAll();
  52.     }
  53.    
  54.     public static void main(String[] args) {
  55.         Teste[] vetor = new Teste[10];
  56.         BlockingQueue<Integer> lista =
  57.                 new BlockingQueue<Integer>(3);
  58.        
  59.         for(int i = 0; i< 10; i++)
  60.         {
  61.             vetor[i] = new Teste(lista, i%2==0);
  62.             vetor[i].start();
  63.         }
  64.        
  65.        
  66.     }
  67.    
  68.    
  69.  
  70. }
  71.  
  72. class Teste extends Thread
  73. {
  74.     private BlockingQueue<Integer> lista;
  75.     private boolean consumer;
  76.    
  77.     public Teste(BlockingQueue<Integer> b, boolean c)
  78.     {
  79.         lista = b;
  80.         consumer = c;
  81.     }
  82.    
  83.     public void run()
  84.     {
  85.         for(int i = 0; i < 10; i++)
  86.         {
  87.             if(consumer)
  88.             {
  89.                 try {
  90.                     Integer e = lista.poll();
  91.                    
  92.                 } catch (InterruptedException e) {
  93.                     // TODO Auto-generated catch block
  94.                     e.printStackTrace();
  95.                 }
  96.             }
  97.             else
  98.             {
  99.                 Integer inte = (int)(Math.random()*100);
  100.                 try {
  101.                     lista.offer(inte);
  102.                 } catch (InterruptedException e) {
  103.                     // TODO Auto-generated catch block
  104.                     e.printStackTrace();
  105.                 }
  106.             }
  107.            
  108.             try {
  109.                 sleep((int)(Math.random() * 1000));
  110.             } catch (InterruptedException e) {
  111.                 // TODO Auto-generated catch block
  112.                 e.printStackTrace();
  113.             }
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement