Advertisement
m4ly

Threads

Nov 29th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mocek;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. /**
  12.  *
  13.  * @author Lab
  14.  */
  15. public class Mocek {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.         User[] users = new User[15];
  22.  
  23.         for (int i = 0; i < users.length; i++) {
  24.             users[i] = new User();
  25.             users[i].start();
  26.         }
  27.  
  28.     }
  29.  
  30. }
  31.  
  32. class Pula {
  33.  
  34.     static int INIT_SIZE = 5;
  35.     static int MAX_SIZE = 10;
  36.  
  37.     List<Zasob> zasob = new ArrayList(INIT_SIZE);
  38.    
  39.     private static final Pula pula = new Pula();
  40.  
  41.     public static Pula getPula() {
  42.         return pula;
  43.     }
  44.  
  45.     private Pula() {
  46.  
  47.         for (int i = 0; i < INIT_SIZE; i++) {
  48.             zasob.add(new Zasob());
  49.         }
  50.     }
  51.  
  52.     synchronized void put(Zasob z) {
  53.         z.setResourceEmpty(!z.isResourceEmpty());
  54.         notify();
  55.     }
  56.  
  57.     /**
  58.      * pobranie liczby z zasobu
  59.      *
  60.      * @return liczba przechowywana w zasobie
  61.      */
  62.     synchronized Zasob get() {
  63.         while (true) {
  64.             for (int i = 0; i < zasob.size(); i++) {
  65.                 if (zasob.get(i).isResourceEmpty()) {
  66.                     zasob.get(i).setResourceEmpty(false);
  67.                     return zasob.get(i);
  68.                 }
  69.             }
  70.  
  71.             if (zasob.size() < MAX_SIZE) {
  72.                 zasob.add(new Zasob());
  73.                 zasob.get(zasob.size() - 1).setResourceEmpty(false);
  74.                 return zasob.get(zasob.size() - 1);
  75.  
  76.             }
  77.             try {
  78.                 System.err.println("Czekam na zasób");
  79.                 wait();
  80.                 System.err.println("Doczekałem się na zasób");
  81.             } catch (InterruptedException e) {
  82.  
  83.             }
  84.         }
  85.  
  86.     }
  87.  
  88. }
  89.  
  90. class Zasob {
  91.  
  92.     /**
  93.      * true gdy pusty
  94.      */
  95.     private boolean resourceEmpty = true;
  96.  
  97.     /**
  98.      * konstruktor bezparametryczny
  99.      */
  100.     public Zasob() {
  101.  
  102.     }
  103.  
  104.     /**
  105.      * @return the resourceEmpty
  106.      */
  107.     public boolean isResourceEmpty() {
  108.         return resourceEmpty;
  109.     }
  110.  
  111.     /**
  112.      * @param resourceEmpty the resourceEmpty to set
  113.      */
  114.     public void setResourceEmpty(boolean resourceEmpty) {
  115.         this.resourceEmpty = resourceEmpty;
  116.     }
  117. }
  118.  
  119. class User extends Thread {
  120.     @Override
  121.     public void run() {
  122.         Zasob z = Pula.getPula().get();
  123.  
  124.         System.out.println("Dostałem zasób " + z);
  125.  
  126.         try {
  127.             Thread.sleep(300);
  128.         } catch (InterruptedException ex) {
  129.  
  130.         }
  131.  
  132.         System.out.println("Zwalniam zasób " + z);
  133.         Pula.getPula().put(z);
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement