Advertisement
majczel23000

[PWJJ] Wątki - bilety

Nov 27th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. package lab3;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.concurrent.Callable;
  6. import java.util.concurrent.ExecutionException;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9. import java.util.concurrent.Future;
  10. import java.util.concurrent.locks.Condition;
  11. import java.util.concurrent.locks.Lock;
  12. import java.util.concurrent.locks.ReentrantLock;
  13.  
  14.  
  15. class Bilet{
  16.  
  17.     int numer;  // numer biletu
  18.     boolean czyZarezerwowany = false;   // false - nie, true - tak
  19.     Lock lock = new ReentrantLock();
  20.     Condition biletZarezerwowany = lock.newCondition();
  21.     Condition biletZwolniony = lock.newCondition();
  22.    
  23.     // konstruktor
  24.     Bilet(int n){
  25.         numer= n;
  26.     }
  27.    
  28.     //Metoda rezerwująca bilet
  29.     void zarezerwuj() {
  30.         lock.lock();
  31.         try {
  32.             while (czyZarezerwowany == true)
  33.                 biletZarezerwowany.await();
  34.             czyZarezerwowany = true;
  35.             System.out.println("Wątek: " + Thread.currentThread().getName() + " zarezerwował bilet: " + numer);
  36.             biletZwolniony.signal();
  37.         } catch(InterruptedException exc) {
  38.            
  39.         } finally {
  40.             lock.unlock();
  41.         }
  42.     }
  43.    
  44.     //Metoda zwalniająca bilet
  45.     void zwolnij() {
  46.         lock.lock();
  47.         try {
  48.             while (czyZarezerwowany == false)
  49.                 biletZwolniony.await();
  50.             czyZarezerwowany = false;
  51.             System.out.println("Wątek: " + Thread.currentThread().getName() + " zwolnił bilet: " + numer);
  52.             biletZarezerwowany.signal();
  53.         } catch(InterruptedException exc) {
  54.            
  55.         } finally {
  56.             lock.unlock();
  57.         }
  58.     }
  59. }
  60.  
  61. class Kupiec implements Callable<Integer>{
  62.    
  63.     List<Bilet> bilety;
  64.    
  65.     Kupiec(List<Bilet> b) {
  66.         bilety = b;
  67.     }
  68.    
  69.     public Integer call() {
  70.         for (int i = 0; i < 10; i++) {
  71.             for(int j = 0; j < bilety.size(); j++) {
  72.                
  73.                 bilety.get(j).zarezerwuj();
  74.                 try { // autor zastanawia się chwilę co napisać
  75.                     Thread.sleep((int) (Math.random()* 10000));
  76.                 } catch (InterruptedException exc) {
  77.                 }
  78.                 bilety.get(j).zwolnij();
  79.             }
  80.         }
  81.         return 1;
  82.     }
  83. }
  84.  
  85. public class Zadaniee {
  86.    
  87.     public static int wykonuj(ExecutorService exec, List<Callable<Integer>> tasks) throws Exception{
  88.         List<Future<Integer>> results = exec.invokeAll(tasks);
  89.         return 1;
  90.     }
  91.  
  92.     public static void main(String[] args) {
  93.         // TODO Auto-generated method stub
  94.         List<Bilet> bilety = new ArrayList<Bilet>();
  95.         bilety.add(new Bilet(1));
  96.         bilety.add(new Bilet(2));
  97.         bilety.add(new Bilet(3));
  98.        
  99.         List<Callable<Integer>> kupcy = new ArrayList<Callable<Integer>>();
  100.         ExecutorService exec = Executors.newFixedThreadPool(6);
  101.         kupcy.add(new Kupiec(bilety));
  102.         kupcy.add(new Kupiec(bilety));
  103.         kupcy.add(new Kupiec(bilety));
  104.         kupcy.add(new Kupiec(bilety));
  105.         kupcy.add(new Kupiec(bilety));
  106.         kupcy.add(new Kupiec(bilety));
  107.         try {
  108.             int result = wykonuj(exec, kupcy);
  109.             exec.shutdown();
  110.         } catch(Exception exc) {
  111.             exc.printStackTrace();
  112.         }
  113.     }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement