Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. import java.util.GregorianCalendar;
  2. import java.util.Random;
  3.  
  4. /* aplikacja implementująca problem producenta i konsumenta,
  5. producent "produkuje" liczbę i umieszcza ją w zasobie, w
  6. zasobie może być tylko jedna liczba */
  7.  
  8. /** główna klasa programu */
  9. public class PCProblem1
  10. {
  11.     /** główna metoda aplikacji */
  12.     public static void main(String args[])
  13.     {
  14.         // utwórz zasób
  15.         Resource resource = new Resource();
  16.        
  17.         // utwórz producenta i konsumenta
  18.         new Producer(resource);
  19.         new Consumer(resource);
  20.     }
  21. }
  22.  
  23. /** klasa producenta */
  24. class Producer implements Runnable
  25. {
  26.     /** zasób */
  27.     Resource resource;     
  28.  
  29.     /** konstruktor
  30.      *  @param resource referencja do zasobu */
  31.     Producer(Resource resource)
  32.     {
  33.         this.resource = resource;
  34.  
  35.         // uruchom wątek producenta
  36.         new Thread(this).start();
  37.     }
  38.  
  39.     /** metoda run wątku producenta */
  40.     public void run()
  41.     {
  42.         // utworzenie generatora liczb pseudolosowych
  43.         Random rnd = new Random();
  44.         rnd.setSeed((new GregorianCalendar()).getTimeInMillis());
  45.        
  46.         // umieszczanie liczb w zasobie
  47.         int number;
  48.         do
  49.         {
  50.             try
  51.             {
  52.                 Thread.sleep(500);      // uśpij producenta na 500 milisekund
  53.             }
  54.             catch(InterruptedException e)
  55.             {}
  56.            
  57.             number = rnd.nextInt() % 100;
  58.            
  59.             System.out.println("Liczba " + number + " została wyprodukowana");
  60.            
  61.             resource.put(number);
  62.            
  63.             System.out.println("Liczba " + number + " została wstawiona do zasobu");
  64.         }
  65.         while(number > 0);
  66.        
  67.         System.out.println("Producent zakończyć pracę");                
  68.     }
  69. }
  70.  
  71. /** klasa konsumenta */
  72. class Consumer implements Runnable
  73. {
  74.     /** zasób */
  75.     Resource resource; 
  76.  
  77.     /** konstruktor
  78.      *  @param resource referencja do zasobu */
  79.     Consumer(Resource resource)
  80.     {
  81.         this.resource = resource;
  82.  
  83.         // uruchom wątek konsumenta
  84.         new Thread(this).start();
  85.     }
  86.  
  87.     /** metoda run wątku konsumenta */
  88.     public void run()
  89.     {
  90.         int number;
  91.        
  92.         do
  93.         {  
  94.             try
  95.             {
  96.                 Thread.sleep(50);       // uśpij konsumenta na 50 milisekund
  97.             }
  98.             catch(InterruptedException e)
  99.             {}
  100.            
  101.             // pobierz liczbę z zasobu
  102.             number = resource.get();
  103.            
  104.             System.out.println("Liczba " + number + " została skonsumowana");
  105.         }
  106.         while(number > 0);
  107.     }
  108. }
  109.  
  110. /** klasa zasobu */
  111. class Resource  
  112. {
  113.     /** przechowywana liczba */
  114.     int number;
  115.    
  116.     /** true gdy zasób pusty */
  117.     boolean resourceEmpty = true;
  118.    
  119.     /** konstruktor bezparametryczny */
  120.     public Resource()
  121.     {
  122.     }
  123.    
  124.     /** umieszczanie liczby w zasobie
  125.      *  @param number liczba umieszczana w zasobie */
  126.     synchronized void put(int number)
  127.     {
  128.         if(!resourceEmpty)          // zasób nie jest pusty
  129.         {
  130.             try
  131.             {
  132.                 System.out.println("Producent czeka...");
  133.                
  134.                 wait();     // czekaj aż zasób będzie pusty
  135.             }
  136.             catch(InterruptedException e)
  137.             {}
  138.         }
  139.        
  140.         // umieść liczbę w zasobie
  141.         this.number = number;
  142.        
  143.         resourceEmpty = false;
  144.  
  145.         notify();
  146.     }
  147.  
  148.     /** pobranie liczby z zasobu
  149.      *  @return liczba przechowywana w zasobie */
  150.     synchronized int get()
  151.     {
  152.         if(resourceEmpty)       // zasób jest pusty
  153.         {
  154.             try
  155.             {
  156.                 System.out.println("Konsument czeka...");
  157.                
  158.                 wait();         // czekaj aż pojawi się liczba w zasobie
  159.             }  
  160.             catch( InterruptedException e )
  161.             {}
  162.         }  
  163.        
  164.         // pobierz liczbę z zasobu
  165.         int number = this.number;
  166.  
  167.         resourceEmpty = true;
  168.  
  169.         notify();
  170.  
  171.         return number;
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement