Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- 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;
- import java.util.concurrent.atomic.*;
- public class Louvre {
- private Obilasci o = new Obilasci();
- private class Obilasci {
- AtomicInteger nemac = new AtomicInteger();
- AtomicInteger italijan = new AtomicInteger();
- AtomicInteger englez = new AtomicInteger();
- public void preNemac() {
- boolean ok = true;
- do {
- if (!ok)
- Thread.yield();
- int staraVr = nemac.get();
- int novaVr = staraVr + 1;
- ok = (italijan.get() == 0) && (englez.get() == 0);
- if (ok)
- ok = nemac.compareAndSet(staraVr, novaVr);
- } while (!ok);
- }
- public void posleNemac() {
- nemac.decrementAndGet();
- }
- public void preItalijan() {
- boolean ok = true;
- do {
- if (!ok)
- Thread.yield();
- int staraVr = italijan.get();
- int novaVr = staraVr + 1;
- ok = (nemac.get() == 0) && (englez.get() == 0);
- if (ok)
- ok = italijan.compareAndSet(staraVr, novaVr);
- } while (!ok);
- }
- public void posleItalijan() {
- italijan.decrementAndGet();
- }
- public void preEnglez() {
- boolean ok = true;
- do {
- if (!ok)
- Thread.yield();
- int staraVr = englez.get();
- int novaVr = staraVr + 1;
- ok = (italijan.get() == 0) && (nemac.get() == 0);
- if (ok)
- ok = englez.compareAndSet(staraVr, novaVr);
- } while (!ok);
- }
- public void posleEnglez() {
- englez.decrementAndGet();
- }
- }
- private class Englez extends UtilThread {
- public Englez(int id) {
- super("\u0415\u043d\u0433\u043b\u0435\u0437 " + id, BOJA_ENGLEZ, odmaraEnglez);
- }
- @Override
- protected void step() {
- odmara();
- o.preEnglez();
- obilazi();
- o.posleEnglez();
- }
- }
- private class Nemac extends UtilThread {
- public Nemac(int id) {
- super("\u041d\u0435\u043c\u0430\u0446 " + id, BOJA_NEMAC, odmaraNemac);
- }
- @Override
- protected void step() {
- odmara();
- o.preNemac();
- obilazi();
- o.posleNemac();
- }
- }
- private class Italijan extends UtilThread {
- public Italijan(int id) {
- super("\u0418\u0442\u0430\u043b\u0438\u0458\u0430\u043d " + id, BOJA_ITALIJAN, odmaraItalijan);
- }
- @Override
- protected void step() {
- odmara();
- o.preItalijan();
- obilazi();
- o.posleItalijan();
- }
- }
- // Parametri simulacije
- public static final int DUZINA_OBILASKA = 3000;
- public static final int DUZINA_ODMARANJA = 5000;
- public static final int BROJ_DJAKA = 8;
- // Boje
- public static final Color BOJA_ENGLEZ = new Color(0xFF, 0x44, 0x44);
- public static final Color BOJA_NEMAC = new Color(0x22, 0x22, 0x22);
- public static final Color BOJA_ITALIJAN = new Color(0x44, 0xCC, 0x44);
- public static final Color BOJA_POZ_ENGLEZI = new Color(0xFF, 0x44, 0x44);
- public static final Color BOJA_POZ_NEMCI = new Color(0x44, 0x44, 0x44);
- public static final Color BOJA_POZ_ITALIJANI = new Color(0x44, 0xCC, 0x44);
- public static final Color BOJA_MUZEJA = null;
- // Stringovi
- public static final String TEXT_OBILAZI = "\u041e\u0431\u0438\u043b\u0430\u0437\u0438";
- public static final String TEXT_OBISAO = "\u041e\u0431\u0438\u0448\u0430\u043e";
- public static final String TEXT_ODMARA = "\u041E\u0434\u043C\u0430\u0440\u0430";
- public static final String TEXT_ODMORIO = "\u0421\u043F\u0440\u0435\u043C\u0430\u043D";
- public static final String TEXT_GRUPA_ENGLEZI = "\u0413\u0440\u0443\u043f\u0430\u0020\u0415\u043d\u0433\u043b\u0435\u0437\u0430";
- public static final String TEXT_GRUPA_NEMCI = "\u0413\u0440\u0443\u043f\u0430\u0020\u041d\u0435\u043c\u0430\u0446\u0430";
- public static final String TEXT_GRUPA_ITALIJANI = "\u0413\u0440\u0443\u043f\u0430\u0020\u0418\u0442\u0430\u043b\u0438\u0458\u0430\u043d\u0430";
- public static final String TEXT_MUZEJ = "\u041c\u0443\u0437\u0435\u0458 \"Louvre\"";
- private class UtilThread extends SimulationThread {
- private SimulationContainer odmara;
- protected UtilThread(String name, Color color, SimulationContainer klupa) {
- super(name, color);
- odmara = klupa;
- }
- protected void odmara() {
- setContainer(odmara);
- setText(TEXT_ODMARA);
- work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
- setText(TEXT_ODMORIO);
- }
- protected void obilazi() {
- setContainer(obilazi);
- setText(TEXT_OBILAZI);
- work(DUZINA_OBILASKA, 2 * DUZINA_OBILASKA);
- setText(TEXT_OBISAO);
- }
- }
- // Glavni program
- public static void main(String[] a) {
- new Louvre();
- }
- // Stanja
- private SimulationContainer obilazi = new SimulationContainer(TEXT_MUZEJ, BOJA_MUZEJA, SimulationContainerLayout.BOX);
- private SimulationContainer odmaraEnglez = new SimulationContainer(TEXT_GRUPA_ENGLEZI, BOJA_POZ_ENGLEZI, SimulationContainerLayout.BOX);
- private SimulationContainer odmaraNemac = new SimulationContainer(TEXT_GRUPA_NEMCI, BOJA_POZ_NEMCI, SimulationContainerLayout.BOX);
- private SimulationContainer odmaraItalijan = new SimulationContainer(TEXT_GRUPA_ITALIJANI, BOJA_POZ_ITALIJANI, SimulationContainerLayout.BOX);
- private SimulationContainer stajaliste = new SimulationContainer(SimulationContainerLayout.ROW, odmaraNemac, odmaraItalijan, odmaraEnglez);
- private SimulationContainer sve = new SimulationContainer(SimulationContainerLayout.COLUMN, stajaliste, obilazi);
- public Louvre() {
- // Create frame
- SimulationPanel panel = new SwingSimulationPanel(sve);
- SimulationFrame frame = SimulationFrame.create("Louvre", panel, new NoAnimationPanel());
- frame.display();
- // Create threads
- for (int i = 1; i <= BROJ_DJAKA; i++) {
- new Englez(i).start();
- new Nemac(i).start();
- new Italijan(i).start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement