Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.Executors;
- public class HistogramGenerator implements HistogramGeneratorInterface {
- private static DiceInterface dice;
- private HistogramInterface histogram;
- private int threads;
- int numberOfFaces;
- HistogramGenerator() {
- }
- @Override
- public void setHistogram(HistogramInterface histogram) {
- this.histogram = histogram;
- }
- @Override
- public void setDice(DiceInterface dice) {
- this.dice = dice;
- }
- @Override
- public void setNumberOfThreads(int threads) {
- this.threads = threads;
- }
- @Override
- public void start(int numberOfDraws) {
- histogram.setSize(dice.getNumberOfFaces());
- Threads[] watki = new Threads[threads];
- for(int i=0; i<watki.length; i++) {
- watki[i] = new Threads(dice, histogram, numberOfDraws, dice.getNumberOfFaces());
- }
- try {
- for (int i = 0; i < watki.length; i++) {
- watki[i].t.join();
- }
- }catch (InterruptedException e) {
- System.exit(1);
- }
- }
- public static void main(String args[]) {
- }
- }
- class Threads extends HistogramGenerator implements Runnable {
- int wylosowane = 0;
- Thread t;
- private DiceInterface diceInterface;
- private HistogramInterface histogramInterface;
- int numberOfDraws, faces;
- static Object[] uzywaneBiny;
- final Object blokuj = new Object();
- Threads(DiceInterface diceInterface, HistogramInterface histogramInterface, int numberOfDraws, int faces) {
- this.diceInterface = diceInterface;
- this.histogramInterface = histogramInterface;
- this.numberOfDraws = numberOfDraws;
- this.faces = faces;
- uzywaneBiny = new Object[faces];
- for(int i=0; i<faces; i++) {
- uzywaneBiny[i] = new Object();
- }
- t = new Thread(this);
- t.start();
- }
- @Override
- public void run() {
- int draw;
- while(true) {
- //TU BYL LICZNIK
- draw = diceInterface.get();
- synchronized (uzywaneBiny[draw-2]) {
- histogramInterface.increment(draw);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment