Advertisement
Guest User

zad3.1 magazyn

a guest
May 5th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. class Magazyn {
  7.  
  8. // typ czesci
  9. static final int TypA = 0;
  10. static final int TypB = 1;
  11. static final int TypC = 2;
  12. Random r = new Random();
  13.  
  14. public static final int MAXQUEUE = 45;
  15. private List[] mMiejsceNaCzesci;
  16.  
  17. public int ile[] = {0,0,0};
  18.  
  19. public Magazyn(){
  20. mMiejsceNaCzesci = new List[3];
  21. mMiejsceNaCzesci[TypA] = new ArrayList<String>();
  22. mMiejsceNaCzesci[TypB] = new ArrayList<String>();
  23. mMiejsceNaCzesci[TypC] = new ArrayList<String>();
  24. }
  25.  
  26. public synchronized String getCzesc(int typ){
  27.  
  28. // magazyn ma czesci tego typu ?
  29. while (this.ileJestElementow(typ) == 0){
  30. try {
  31. // obudz producenta
  32. System.out.println(" czekam na typ "+typ);
  33. notifyAll();
  34. // czekaj
  35. wait();
  36. } catch(InterruptedException e) { }
  37. }
  38.  
  39. ile[typ]--;
  40. String message = (String)mMiejsceNaCzesci[typ].remove(0);
  41. notify();
  42. return message;
  43. }
  44.  
  45. public synchronized boolean czy_czekac(int typ){
  46. // kolejka pelna
  47. if(this.zajetoscMagazynu() >= Magazyn.MAXQUEUE ) return true;
  48. if(14 < this.ile[typ] ) return true;
  49. // brakuje konkretnej czesci - najgorzej, bo nie mozemy nic wyprodukowac
  50. boolean jest_brak = false;
  51. if((this.ile[0]== 0 || this.ile[1]== 0 || this.ile[2]== 0))
  52. {
  53. jest_brak = true;
  54. }
  55.  
  56. // a ty chesz dac cos co mamy
  57. if(this.ile[typ] > 0 && jest_brak){
  58. return true;
  59. }
  60.  
  61. return false;
  62. }
  63.  
  64. public synchronized void putCzesc(int typ, char slowo[]){
  65. while ( czy_czekac(typ) )
  66. {
  67. try {
  68. wait();
  69. } catch(InterruptedException e){
  70.  
  71. }
  72. }
  73. ile[typ]++;
  74. slowo[2]++;
  75. if(slowo[2]>'z'){
  76. slowo[1]++;
  77. slowo[2]='a';
  78. }
  79. if(slowo[1]>'z'){
  80. slowo[0]++;
  81. slowo[1]='a';
  82. }
  83. if(slowo[0]>'z'){
  84. slowo[0]= 'a';
  85. slowo[1]= 'a';
  86. }
  87. String haslo = new String(slowo);
  88. mMiejsceNaCzesci[typ].add(haslo);
  89. System.out.println("czesc typu " + typ + " wyprodukowana; stan magazynu:["+ile[0]+","+ile[1]+","+ile[2]+"] utilization:"+zajetoscMagazynu());
  90. notify();
  91. }
  92.  
  93. public synchronized int zajetoscMagazynu(){
  94. return (mMiejsceNaCzesci[TypA].size() +
  95. mMiejsceNaCzesci[TypB].size() +
  96. mMiejsceNaCzesci[TypC].size());
  97.  
  98. }
  99.  
  100. public synchronized int ileJestElementow(int typ){
  101. return (mMiejsceNaCzesci[typ].size());
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement