Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 8.11 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.text.DecimalFormat;
  3. import java.util.concurrent.atomic.AtomicInteger;
  4. import java.util.concurrent.atomic.AtomicLong;
  5.  
  6. import os.simulation.SimulationContainer;
  7. import os.simulation.SimulationContainerLayout;
  8. import os.simulation.SimulationThread;
  9. import os.simulation.gui.NoAnimationPanel;
  10. import os.simulation.gui.SimulationFrame;
  11. import os.simulation.gui.SimulationPanel;
  12. import os.simulation.gui.swing.SwingSimulationPanel;
  13.  
  14. public class DecaITrambolina {
  15.    
  16.     private static final int MAX_BROJ = 5;
  17.     private static final double MAX_TEZINA = 300.0;
  18.    
  19.     private Trambolina trambolina = new Trambolina();
  20.    
  21.     private class Trambolina {
  22.        
  23.         private AtomicInteger broj;
  24.         //private AtomicLong tezina;
  25.         private AtomicInteger queue;
  26.        
  27.         public Trambolina() {
  28.             broj = new AtomicInteger(MAX_BROJ);
  29.             //long bits = Double.doubleToLongBits(MAX_TEZINA);
  30.             queue = new AtomicInteger(0);
  31.             //tezina = new AtomicLong(bits);
  32.         }
  33.        
  34.         public void popniSe(double t) {
  35.            
  36.             // Prvo zauzmemo tezinu
  37.             boolean ok;
  38.             do {
  39.                 int curQ = queue.getAndIncrement();
  40.                 int nQ = queue.get();
  41.                /* long staraVrednost = tezina.get();
  42.                 double staraVrednostKaoDouble = Double.longBitsToDouble(staraVrednost);
  43.                 double novaVrednostKaoDouble = staraVrednostKaoDouble - t;
  44.                 long novaVrednost = Double.doubleToLongBits(novaVrednostKaoDouble);
  45.                 ok = novaVrednostKaoDouble >= 0;*/
  46.                 ok = curQ == 0;
  47.                 if (ok) {
  48.                     //ok = tezina.compareAndSet(staraVrednost, novaVrednost);
  49.                     ok = queue.compareAndSet(nQ, curQ);
  50.                 }
  51.                 if (!ok) {
  52.                     Thread.yield();
  53.                 }
  54.             } while (!ok);
  55.            
  56.             // Pa onda mesto
  57.             do {
  58.                 int staraVrednost = broj.get();
  59.                 int novaVrednost = staraVrednost - 1;
  60.                 ok = novaVrednost >= 0;
  61.                 if (ok) {
  62.                     ok = broj.compareAndSet(staraVrednost, novaVrednost);
  63.                 }
  64.                 if (!ok) {
  65.                     Thread.yield();
  66.                 }
  67.             } while (!ok);
  68.            
  69.         }
  70.        
  71.         public void sidji(double t) {
  72.            
  73.            
  74.            
  75.             // Pa tezinu
  76.             boolean ok;
  77.             do {
  78.                 int curQ = queue.getAndDecrement();
  79.                 int nQ = queue.get();
  80.                 /*long staraVrednost = tezina.get();
  81.                 double staraVrednostKaoDouble = Double.longBitsToDouble(staraVrednost);
  82.                 double novaVrednostKaoDouble = staraVrednostKaoDouble + t;
  83.                 long novaVrednost = Double.doubleToLongBits(novaVrednostKaoDouble);
  84.                 ok = tezina.compareAndSet(staraVrednost, novaVrednost);*/
  85.                 ok = queue.compareAndSet(nQ, curQ);
  86.             } while (!ok);
  87.             // Oslobodimo mesto
  88.             broj.incrementAndGet();
  89.         }
  90.     }
  91.    
  92.     private class Dete extends UtilThread {
  93.        
  94.         // Klasa nasledjuje polja
  95.         // boolean pol i double tezina
  96.         // koja se slobodno mogu koristiti
  97.        
  98.         @Override
  99.         protected void step() {
  100.             odmara();
  101.             trambolina.popniSe(tezina);
  102.             skace();
  103.             trambolina.sidji(tezina);
  104.         }
  105.     }
  106.    
  107.     // Parametri simulacije
  108.     public static final int    BROJ_DECE         = 25;
  109.     public static final double PROCENAT_DECAKA   = 0.5;
  110.     public static final double TEZINA_DETETA_MIN = 25;
  111.     public static final double TEZINA_DETETA_MAX = 80;
  112.     public static final int    DUZINA_SKAKANJA   = 3000;
  113.     public static final int    DUZINA_ODMARANJA  = 5000;
  114.     public static final double MAX_IZDRZLJIVOST  = 300;
  115.     public static final double VEROVATNOCA       = 0.2;
  116.    
  117.     // Boje
  118.     public static final Color  BOJA_DECAKA      = new Color(0xCC, 0xCC, 0xFF);
  119.     public static final Color  BOJA_DEVOJCICA   = new Color(0xFF, 0xCC, 0xCC);
  120.     public static final Color  BOJA_VAN         = null;
  121.     public static final Color  BOJA_UNUTRA_OK   = new Color(0xDD, 0xFF, 0xDD);
  122.     public static final Color  BOJA_UNUTRA      = new Color(0xFF, 0xDD, 0xDD);
  123.    
  124.     // Stringovi
  125.     public static final String TEXT_VAN         = "\u041A\u043B\u0443\u043F\u0435";
  126.     public static final String TEXT_UNUTRA      = "\u0422\u0440\u0430\u043C\u0431\u043E\u043B\u0438\u043D\u0430";
  127.     public static final String TEXT_SKACE       = "\u0421\u043A\u0430\u0447\u0435";
  128.     public static final String TEXT_PAO         = "\u041F\u0430\u043E";
  129.     public static final String TEXT_PALA        = "\u041F\u0430\u043B\u0430";
  130.     public static final String TEXT_NE_SKACE    = "\u041D\u0435 \u0441\u043A\u0430\u0447\u0435";
  131.     public static final String TEXT_ODMARA      = "\u041E\u0434\u043C\u0430\u0440\u0430";
  132.     public static final String TEXT_NE_ODMARA   = "\u0427\u0435\u043A\u0430";
  133.    
  134.     private static int countT;
  135.     private static int countF;
  136.     private static double countU;
  137.     private class UtilThread extends SimulationThread {
  138.        
  139.         protected final boolean pol;
  140.         protected final double tezina;
  141.         private String text;
  142.        
  143.         protected UtilThread() {
  144.             pol = Math.random() < PROCENAT_DECAKA;
  145.             tezina = TEZINA_DETETA_MIN + Math.random() * (TEZINA_DETETA_MAX - TEZINA_DETETA_MIN);
  146.             if (pol) {
  147.                 setName("\u041A\u043B\u0438\u043D\u0430\u0446 " + ++countT);
  148.                 setColor(BOJA_DECAKA);
  149.                 text = TEXT_PAO;
  150.             } else {
  151.                 setName("\u041A\u043B\u0438\u043D\u043A\u0430 " + ++countF);
  152.                 setColor(BOJA_DEVOJCICA);
  153.                 text = TEXT_PALA;
  154.             }
  155.         }
  156.        
  157.         protected void skace() {
  158.             update(tezina);
  159.             setContainer(unutra);
  160.             setText(TEXT_SKACE);
  161.             work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
  162.             while (Math.random() < VEROVATNOCA) {
  163.                 setText(text);
  164.                 work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
  165.                 setText(TEXT_SKACE);
  166.                 work(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
  167.             }
  168.             if (isInterrupted()) {
  169.                 setText(text);
  170.                 workUninterruptibly(DUZINA_SKAKANJA, 2 * DUZINA_SKAKANJA);
  171.             }
  172.             setText(TEXT_NE_SKACE);
  173.             update(-tezina);
  174.         }
  175.        
  176.         protected void odmara() {
  177.             setContainer(van);
  178.             setText(TEXT_ODMARA);
  179.             work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
  180.             setText(TEXT_NE_ODMARA);
  181.         }
  182.        
  183.         private void update(double t) {
  184.             countU = countU + t;
  185.             if (countU > MAX_IZDRZLJIVOST) {
  186.                 unutra.setColor(BOJA_UNUTRA);
  187.             } else {
  188.                 unutra.setColor(BOJA_UNUTRA_OK);
  189.             }
  190.         }
  191.     }
  192.    
  193.     // Glavni program
  194.     public static void main(String[] a) {
  195.         new DecaITrambolina();
  196.     }
  197.    
  198.     // Stanja
  199.     private SimulationContainer van = new SimulationContainer(TEXT_VAN, BOJA_VAN, SimulationContainerLayout.BOX);
  200.     private SimulationContainer unutra = new SimulationContainer(TEXT_UNUTRA, BOJA_UNUTRA_OK, SimulationContainerLayout.BOX) {public String getText() {return new DecimalFormat("#,##0.00 kg").format(countU);}};
  201.     private SimulationContainer sve = new SimulationContainer(SimulationContainerLayout.COLUMN, van, unutra);
  202.    
  203.     public DecaITrambolina() {
  204.        
  205.         // Create frame
  206.         SimulationPanel panel = new SwingSimulationPanel(sve);
  207.         SimulationFrame frame = SimulationFrame.create("Trambolina", panel, new NoAnimationPanel());
  208.         frame.display();
  209.        
  210.         // Create threads
  211.         for (int i = 1; i <= BROJ_DECE; i++) {
  212.             new Dete().start();
  213.         }
  214.        
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement