Guest User

Untitled

a guest
Jan 7th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. import java.util.concurrent.Executors;
  2.  
  3. public class HistogramGenerator implements HistogramGeneratorInterface {
  4.  
  5. private static DiceInterface dice;
  6. private HistogramInterface histogram;
  7. private int threads;
  8. int numberOfFaces;
  9.  
  10.  
  11. HistogramGenerator() {
  12.  
  13. }
  14.  
  15. @Override
  16. public void setHistogram(HistogramInterface histogram) {
  17. this.histogram = histogram;
  18. }
  19.  
  20. @Override
  21. public void setDice(DiceInterface dice) {
  22. this.dice = dice;
  23. }
  24.  
  25. @Override
  26. public void setNumberOfThreads(int threads) {
  27. this.threads = threads;
  28. }
  29.  
  30. @Override
  31. public void start(int numberOfDraws) {
  32. histogram.setSize(dice.getNumberOfFaces());
  33. Threads[] watki = new Threads[threads];
  34.  
  35. for(int i=0; i<watki.length; i++) {
  36. watki[i] = new Threads(dice, histogram, numberOfDraws, dice.getNumberOfFaces());
  37. }
  38. try {
  39. for (int i = 0; i < watki.length; i++) {
  40. watki[i].t.join();
  41. }
  42.  
  43. }catch (InterruptedException e) {
  44. System.exit(1);
  45. }
  46.  
  47. }
  48.  
  49. public static void main(String args[]) {
  50.  
  51. }
  52. }
  53.  
  54. class Threads extends HistogramGenerator implements Runnable {
  55. int wylosowane = 0;
  56. Thread t;
  57. private DiceInterface diceInterface;
  58. private HistogramInterface histogramInterface;
  59. int numberOfDraws, faces;
  60. static Object[] uzywaneBiny;
  61. final Object blokuj = new Object();
  62.  
  63. Threads(DiceInterface diceInterface, HistogramInterface histogramInterface, int numberOfDraws, int faces) {
  64. this.diceInterface = diceInterface;
  65. this.histogramInterface = histogramInterface;
  66. this.numberOfDraws = numberOfDraws;
  67. this.faces = faces;
  68.  
  69. uzywaneBiny = new Object[faces];
  70.  
  71. for(int i=0; i<faces; i++) {
  72. uzywaneBiny[i] = new Object();
  73. }
  74. t = new Thread(this);
  75. t.start();
  76. }
  77.  
  78. @Override
  79. public void run() {
  80.  
  81. int draw;
  82. while(true) {
  83. //TU BYL LICZNIK
  84. draw = diceInterface.get();
  85. synchronized (uzywaneBiny[draw-2]) {
  86. histogramInterface.increment(draw);
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment