Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- class Magazyn {
- // typ czesci
- static final int TypA = 0;
- static final int TypB = 1;
- static final int TypC = 2;
- Random r = new Random();
- public static final int MAXQUEUE = 45;
- private List[] mMiejsceNaCzesci;
- public int ile[] = {0,0,0};
- public Magazyn(){
- mMiejsceNaCzesci = new List[3];
- mMiejsceNaCzesci[TypA] = new ArrayList<String>();
- mMiejsceNaCzesci[TypB] = new ArrayList<String>();
- mMiejsceNaCzesci[TypC] = new ArrayList<String>();
- }
- public synchronized String getCzesc(int typ){
- // magazyn ma czesci tego typu ?
- while (this.ileJestElementow(typ) == 0){
- try {
- // obudz producenta
- System.out.println(" czekam na typ "+typ);
- notifyAll();
- // czekaj
- wait();
- } catch(InterruptedException e) { }
- }
- ile[typ]--;
- String message = (String)mMiejsceNaCzesci[typ].remove(0);
- notify();
- return message;
- }
- public synchronized boolean czy_czekac(int typ){
- // kolejka pelna
- if(this.zajetoscMagazynu() >= Magazyn.MAXQUEUE ) return true;
- if(14 < this.ile[typ] ) return true;
- // brakuje konkretnej czesci - najgorzej, bo nie mozemy nic wyprodukowac
- boolean jest_brak = false;
- if((this.ile[0]== 0 || this.ile[1]== 0 || this.ile[2]== 0))
- {
- jest_brak = true;
- }
- // a ty chesz dac cos co mamy
- if(this.ile[typ] > 0 && jest_brak){
- return true;
- }
- return false;
- }
- public synchronized void putCzesc(int typ, char slowo[]){
- while ( czy_czekac(typ) )
- {
- try {
- wait();
- } catch(InterruptedException e){
- }
- }
- ile[typ]++;
- slowo[2]++;
- if(slowo[2]>'z'){
- slowo[1]++;
- slowo[2]='a';
- }
- if(slowo[1]>'z'){
- slowo[0]++;
- slowo[1]='a';
- }
- if(slowo[0]>'z'){
- slowo[0]= 'a';
- slowo[1]= 'a';
- }
- String haslo = new String(slowo);
- mMiejsceNaCzesci[typ].add(haslo);
- System.out.println("czesc typu " + typ + " wyprodukowana; stan magazynu:["+ile[0]+","+ile[1]+","+ile[2]+"] utilization:"+zajetoscMagazynu());
- notify();
- }
- public synchronized int zajetoscMagazynu(){
- return (mMiejsceNaCzesci[TypA].size() +
- mMiejsceNaCzesci[TypB].size() +
- mMiejsceNaCzesci[TypC].size());
- }
- public synchronized int ileJestElementow(int typ){
- return (mMiejsceNaCzesci[typ].size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement