Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.GregorianCalendar;
- import java.util.Random;
- /* aplikacja implementująca problem producenta i konsumenta,
- producent "produkuje" liczbę i umieszcza ją w zasobie, w
- zasobie może być tylko jedna liczba */
- /** główna klasa programu */
- public class PCProblem1
- {
- /** główna metoda aplikacji */
- public static void main(String args[])
- {
- // utwórz zasób
- Resource resource = new Resource();
- // utwórz producenta i konsumenta
- new Producer(resource);
- new Consumer(resource);
- }
- }
- /** klasa producenta */
- class Producer implements Runnable
- {
- /** zasób */
- Resource resource;
- /** konstruktor
- * @param resource referencja do zasobu */
- Producer(Resource resource)
- {
- this.resource = resource;
- // uruchom wątek producenta
- new Thread(this).start();
- }
- /** metoda run wątku producenta */
- public void run()
- {
- // utworzenie generatora liczb pseudolosowych
- Random rnd = new Random();
- rnd.setSeed((new GregorianCalendar()).getTimeInMillis());
- // umieszczanie liczb w zasobie
- int number;
- do
- {
- try
- {
- Thread.sleep(500); // uśpij producenta na 500 milisekund
- }
- catch(InterruptedException e)
- {}
- number = rnd.nextInt() % 100;
- System.out.println("Liczba " + number + " została wyprodukowana");
- resource.put(number);
- System.out.println("Liczba " + number + " została wstawiona do zasobu");
- }
- while(number > 0);
- System.out.println("Producent zakończyć pracę");
- }
- }
- /** klasa konsumenta */
- class Consumer implements Runnable
- {
- /** zasób */
- Resource resource;
- /** konstruktor
- * @param resource referencja do zasobu */
- Consumer(Resource resource)
- {
- this.resource = resource;
- // uruchom wątek konsumenta
- new Thread(this).start();
- }
- /** metoda run wątku konsumenta */
- public void run()
- {
- int number;
- do
- {
- try
- {
- Thread.sleep(50); // uśpij konsumenta na 50 milisekund
- }
- catch(InterruptedException e)
- {}
- // pobierz liczbę z zasobu
- number = resource.get();
- System.out.println("Liczba " + number + " została skonsumowana");
- }
- while(number > 0);
- }
- }
- /** klasa zasobu */
- class Resource
- {
- /** przechowywana liczba */
- int number;
- /** true gdy zasób pusty */
- boolean resourceEmpty = true;
- /** konstruktor bezparametryczny */
- public Resource()
- {
- }
- /** umieszczanie liczby w zasobie
- * @param number liczba umieszczana w zasobie */
- synchronized void put(int number)
- {
- if(!resourceEmpty) // zasób nie jest pusty
- {
- try
- {
- System.out.println("Producent czeka...");
- wait(); // czekaj aż zasób będzie pusty
- }
- catch(InterruptedException e)
- {}
- }
- // umieść liczbę w zasobie
- this.number = number;
- resourceEmpty = false;
- notify();
- }
- /** pobranie liczby z zasobu
- * @return liczba przechowywana w zasobie */
- synchronized int get()
- {
- if(resourceEmpty) // zasób jest pusty
- {
- try
- {
- System.out.println("Konsument czeka...");
- wait(); // czekaj aż pojawi się liczba w zasobie
- }
- catch( InterruptedException e )
- {}
- }
- // pobierz liczbę z zasobu
- int number = this.number;
- resourceEmpty = true;
- notify();
- return number;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement