Advertisement
Guest User

Untitled

a guest
May 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 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 pr4;
  7. import java.util.Scanner;
  8. /**
  9.  *
  10.  * @author student
  11.  */
  12. class Bufor
  13. {
  14.     final int rozmiar=10000;
  15.     long bufor[]=new long[rozmiar];
  16.     int mp, mk, zapas;
  17. }
  18. class ProdKons extends Thread
  19. {
  20.     int nr;
  21.     public ProdKons(int nr)
  22.     {
  23.         this.nr=nr;
  24.         if(nr<20)
  25.             this.setName("Producent:"+nr);
  26.         else
  27.             this.setName("Konsument:"+nr);
  28.     }
  29.     static Bufor buf=new Bufor();
  30.     static long t0;
  31.     static void rozgrzewka(int pmp, int pmk, int pzapas)
  32.     {
  33.         buf.mp=pmp;
  34.         buf.mk=pmk;
  35.         buf.zapas=pzapas;
  36.         t0=System.currentTimeMillis();
  37.     }
  38.     synchronized void producent()
  39.     {
  40.         if(buf.zapas<buf.rozmiar)
  41.         {long czas=(long)Math.round(Math.random()*2);
  42.             try
  43.             {this.sleep(czas+5);}
  44.         catch(Exception e){System.out.print(" wyjątek - "+e);}
  45.     }
  46.         long nr=(long)Math.round(Math.random()*100000);
  47.         buf.bufor[buf.mp]=nr;
  48.         buf.mp=(buf.mp+1)%buf.rozmiar;
  49.         buf.zapas++;
  50.     }
  51.     synchronized void konsument()
  52.     {
  53.         if(buf.zapas>0)
  54.         {
  55.             long konsumcja=buf.bufor[buf.mk];
  56.             buf.mk=(buf.mk+1)%buf.rozmiar;
  57.             buf.zapas--;
  58.             long czas=(long)(5+Math.round(Math.random()*10));
  59.             try
  60.             {
  61.                 sleep(czas);
  62.             }
  63.             catch(Exception E){System.out.print(E);}
  64.         }
  65.     }
  66.     public void run()
  67.     {
  68.         long tx=t0;
  69.         while(tx<t0+10000)
  70.         {
  71.             if(nr<20)producent();
  72.             else konsument();
  73.             tx=System.currentTimeMillis();
  74.             raport();
  75.         }
  76.     }
  77.     void raport()
  78.     {
  79.         if(nr<20)
  80.         System.out.println("Produkcja : "+this.getName());
  81.         else
  82.             System.out.println("Konsumpcja : "+this.getName());
  83.     }
  84. }
  85.  
  86. public class Pr4 {
  87.  
  88.     /**
  89.      * @param args the command line arguments
  90.      */
  91.     public static void main(String[] args) {
  92.         ProdKons.rozgrzewka(0, 0, 0);
  93.         ProdKons P[]=new ProdKons[20];
  94.         ProdKons K[]=new ProdKons[1000];
  95.         for(int i=0; i<20; i++)P[i]=new ProdKons(i);
  96.         for(int i=0; i<1000; i++)K[i]=new ProdKons(i+20);
  97.         for(int i=0; i<20; i++)P[i].start();
  98.         //for(int i=0; i<1000; i++)K[i].start();
  99.         for(int i=0; i<20; i++)
  100.             try
  101.             {
  102.                 P[i].join();
  103.             }
  104.         catch(InterruptedExpection e){System.out.printf(e);}
  105.         /*for(int i=0; i<1000; i++)
  106.             try
  107.             {
  108.                 K[i].join();
  109.             }
  110.         catch(InterruptedExpection e){System.out.printf(e);}*/
  111.         // TODO code application logic here
  112.     }
  113.    
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement