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.atomic.AtomicInteger;
- import java.util.concurrent.atomic.AtomicLong;
- 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 {
- private static final int MAX_BROJ = 5;
- private static final double MAX_TEZINA = 300.0;
- private Trambolina trambolina = new Trambolina();
- private class Trambolina {
- private AtomicInteger broj;
- //private AtomicLong tezina;
- private AtomicInteger queue;
- public Trambolina() {
- broj = new AtomicInteger(MAX_BROJ);
- //long bits = Double.doubleToLongBits(MAX_TEZINA);
- queue = new AtomicInteger(0);
- //tezina = new AtomicLong(bits);
- }
- public void popniSe(double t) {
- // Prvo zauzmemo tezinu
- boolean ok;
- do {
- int curQ = queue.getAndIncrement();
- int nQ = queue.get();
- /* long staraVrednost = tezina.get();
- double staraVrednostKaoDouble = Double.longBitsToDouble(staraVrednost);
- double novaVrednostKaoDouble = staraVrednostKaoDouble - t;
- long novaVrednost = Double.doubleToLongBits(novaVrednostKaoDouble);
- ok = novaVrednostKaoDouble >= 0;*/
- ok = curQ == 0;
- if (ok) {
- //ok = tezina.compareAndSet(staraVrednost, novaVrednost);
- ok = queue.compareAndSet(nQ, curQ);
- }
- if (!ok) {
- Thread.yield();
- }
- } while (!ok);
- // Pa onda mesto
- do {
- int staraVrednost = broj.get();
- int novaVrednost = staraVrednost - 1;
- ok = novaVrednost >= 0;
- if (ok) {
- ok = broj.compareAndSet(staraVrednost, novaVrednost);
- }
- if (!ok) {
- Thread.yield();
- }
- } while (!ok);
- }
- public void sidji(double t) {
- // Pa tezinu
- boolean ok;
- do {
- int curQ = queue.getAndDecrement();
- int nQ = queue.get();
- /*long staraVrednost = tezina.get();
- double staraVrednostKaoDouble = Double.longBitsToDouble(staraVrednost);
- double novaVrednostKaoDouble = staraVrednostKaoDouble + t;
- long novaVrednost = Double.doubleToLongBits(novaVrednostKaoDouble);
- ok = tezina.compareAndSet(staraVrednost, novaVrednost);*/
- ok = queue.compareAndSet(nQ, curQ);
- } while (!ok);
- // Oslobodimo mesto
- broj.incrementAndGet();
- }
- }
- private class Dete extends UtilThread {
- // Klasa nasledjuje polja
- // boolean pol i double tezina
- // koja se slobodno mogu koristiti
- @Override
- protected void step() {
- odmara();
- trambolina.popniSe(tezina);
- skace();
- trambolina.sidji(tezina);
- }
- }
- // 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 double MAX_IZDRZLJIVOST = 300;
- public static final double VEROVATNOCA = 0.2;
- // Boje
- public static final Color BOJA_DECAKA = new Color(0xCC, 0xCC, 0xFF);
- public static final Color BOJA_DEVOJCICA = new Color(0xFF, 0xCC, 0xCC);
- 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);
- // Stringovi
- 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";
- private static int countT;
- private static int countF;
- private static double countU;
- private class UtilThread extends SimulationThread {
- protected final boolean pol;
- protected final double tezina;
- private String text;
- protected UtilThread() {
- pol = Math.random() < PROCENAT_DECAKA;
- tezina = TEZINA_DETETA_MIN + Math.random() * (TEZINA_DETETA_MAX - TEZINA_DETETA_MIN);
- if (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;
- }
- }
- 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(TEXT_ODMARA);
- work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
- setText(TEXT_NE_ODMARA);
- }
- private void update(double t) {
- countU = countU + t;
- if (countU > MAX_IZDRZLJIVOST) {
- unutra.setColor(BOJA_UNUTRA);
- } else {
- unutra.setColor(BOJA_UNUTRA_OK);
- }
- }
- }
- // Glavni program
- public static void main(String[] a) {
- new DecaITrambolina();
- }
- // Stanja
- private SimulationContainer van = new SimulationContainer(TEXT_VAN, BOJA_VAN, SimulationContainerLayout.BOX);
- private SimulationContainer unutra = new SimulationContainer(TEXT_UNUTRA, BOJA_UNUTRA_OK, SimulationContainerLayout.BOX) {public String getText() {return new DecimalFormat("#,##0.00 kg").format(countU);}};
- private 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