Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.text.DecimalFormat;
- import java.util.concurrent.Semaphore;
- import os.simulation.SimulationContainer;
- import os.simulation.SimulationContainerLayout;
- import os.simulation.SimulationThread;
- import os.simulation.gui.NoAnimationPanel;
- import os.simulation.gui.SimulationFrame;
- import os.simulation.gui.SimulationPanel;
- import os.simulation.gui.swing.SwingSimulationPanel;
- public class DecaITrambolina {
- public Trambolina trambolina = new Trambolina (MAX_IZDRZLJIVOST_KG, MAX_IZDRZLJIVOST_BR);
- private class Trambolina {
- private Semaphore[] mutexi;
- private int[] brojaci;
- private Semaphore slobodno;
- private Semaphore tezina;
- private Semaphore osobe;
- public Trambolina (int maxTezina, int maxOsoba) {
- mutexi = new Semaphore[] {
- new Semaphore(1, false),
- new Semaphore(1, true)
- };
- brojaci = new int[] {
- 0, 0
- };
- slobodno = new Semaphore(1);
- tezina = new Semaphore(maxTezina);
- osobe = new Semaphore(maxOsoba);
- }
- public void popniSe(Dete d) {
- int pol = d.pol == Pol.MUSKI ? 1 : 0;
- mutexi[pol].acquireUninterruptibly();
- brojaci[pol]++;
- if (brojaci[pol] == 1)
- slobodno.acquireUninterruptibly();
- mutexi[pol].release();
- tezina.acquireUninterruptibly(d.tezina);
- osobe.acquireUninterruptibly(1);
- }
- public void sidji(Dete d) {
- int pol = d.pol == Pol.MUSKI ? 1 : 0;
- mutexi[pol].acquireUninterruptibly();
- brojaci[pol]--;
- if (brojaci[pol] == 0)
- slobodno.release();
- mutexi[pol].release();
- tezina.release(d.tezina);
- osobe.release(1);
- }
- }
- private class Dete extends UtilThread {
- private final Pol pol = getPol();
- private final int tezina = getTezina();
- @Override
- protected void step() {
- odmara();
- trambolina.popniSe(this);
- try {
- skace();
- } finally {
- trambolina.sidji(this);
- }
- // TODO Sinhronizacija
- }
- }
- private enum Pol {
- MUSKI, ZENSKI;
- }
- // -------------------- //
- // Parametri simulacije //
- // -------------------- //
- public static final int BROJ_DECE = 25;
- public static final double PROCENAT_DECAKA = 0.5;
- public static final double TEZINA_DETETA_MIN = 25;
- public static final double TEZINA_DETETA_MAX = 80;
- public static final int DUZINA_SKAKANJA = 3000;
- public static final int DUZINA_ODMARANJA = 5000;
- public static final int MAX_IZDRZLJIVOST_KG = 300;
- public static final int MAX_IZDRZLJIVOST_BR = 5;
- public static final double VEROVATNOCA = 0.1;
- public static final Color BOJA_DECAKA = new Color(0xCC, 0xE6, 0xFF);
- public static final Color BOJA_DEVOJCICA = new Color(0xFF, 0xCC, 0xE6);
- public static final Color BOJA_VAN = null;
- public static final Color BOJA_UNUTRA_OK = new Color(0xDD, 0xFF, 0xDD);
- public static final Color BOJA_UNUTRA = new Color(0xFF, 0xDD, 0xDD);
- public static final String TEXT_VAN = "\u041A\u043B\u0443\u043F\u0435";
- public static final String TEXT_UNUTRA = "\u0422\u0440\u0430\u043C\u0431\u043E\u043B\u0438\u043D\u0430";
- public static final String TEXT_SKACE = "\u0421\u043A\u0430\u0447\u0435";
- public static final String TEXT_PAO = "\u041F\u0430\u043E";
- public static final String TEXT_PALA = "\u041F\u0430\u043B\u0430";
- public static final String TEXT_NE_SKACE = "\u041D\u0435 \u0441\u043A\u0430\u0447\u0435";
- public static final String TEXT_ODMARA = "\u041E\u0434\u043C\u0430\u0440\u0430";
- public static final String TEXT_NE_ODMARA = "\u0427\u0435\u043A\u0430";
- // ------------------- //
- // Sistemski deo //
- // ------------------- //
- // Ne dirati kod ispod //
- // ------------------- //
- private static int countT;
- private static int countF;
- private static double countU;
- private static int countN;
- private class UtilThread extends SimulationThread {
- private final Pol pol;
- private final int tezina;
- private String text;
- protected UtilThread() {
- setShowTime(true);
- pol = Math.random() < PROCENAT_DECAKA ? Pol.MUSKI : Pol.ZENSKI;
- tezina = (int) (TEZINA_DETETA_MIN + Math.random() * (TEZINA_DETETA_MAX - TEZINA_DETETA_MIN));
- if (Pol.MUSKI == pol) {
- setName("\u041A\u043B\u0438\u043D\u0430\u0446 " + ++countT);
- setColor(BOJA_DECAKA);
- text = TEXT_PAO;
- } else {
- setName("\u041A\u043B\u0438\u043D\u043A\u0430 " + ++countF);
- setColor(BOJA_DEVOJCICA);
- text = TEXT_PALA;
- }
- }
- public final Pol getPol() {
- return pol;
- }
- public final int getTezina() {
- return tezina;
- }
- protected void skace() {
- update(tezina);
- setContainer(unutra);
- setText(TEXT_SKACE);
- work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
- while (Math.random() < VEROVATNOCA) {
- setText(text);
- work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
- setText(TEXT_SKACE);
- work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
- }
- if (isInterrupted()) {
- setText(text);
- workUninterruptibly(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
- }
- setText(TEXT_NE_SKACE);
- update(-tezina);
- }
- protected void odmara() {
- setContainer(van);
- setText(convert(tezina));
- work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
- setText(convert(tezina));
- }
- private void update(double t) {
- countU = countU + t;
- countN = countN + (int) Math.signum(t);
- if (countU > MAX_IZDRZLJIVOST_KG || countN > MAX_IZDRZLJIVOST_BR) {
- unutra.setColor(BOJA_UNUTRA);
- } else {
- unutra.setColor(BOJA_UNUTRA_OK);
- }
- }
- }
- // Glavni program
- public static void main(String[] a) {
- new DecaITrambolina();
- }
- private static final DecimalFormat format = new DecimalFormat("#,##0.00 kg");
- private static String convert(double value) {
- return format.format(value);
- }
- // Stanja
- private final SimulationContainer van = new SimulationContainer(TEXT_VAN, BOJA_VAN, SimulationContainerLayout.BOX);
- private final SimulationContainer unutra = new SimulationContainer(TEXT_UNUTRA, BOJA_UNUTRA_OK, SimulationContainerLayout.BOX) {public String getText() {return convert(countU) + " / " + countN;}};
- private final SimulationContainer sve = new SimulationContainer(SimulationContainerLayout.COLUMN, van, unutra);
- public DecaITrambolina() {
- // Create frame
- SimulationPanel panel = new SwingSimulationPanel(sve);
- SimulationFrame frame = SimulationFrame.create("Trambolina", panel, new NoAnimationPanel());
- frame.display();
- // Create threads
- for (int i = 1; i <= BROJ_DECE; i++) {
- new Dete().start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement