Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vezbe5semaphore;
- import java.awt.Color;
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- 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 LouvreLock {
- public final int BROJ_NACIJA = 3;
- private Muzej m = new Muzej();
- private class Muzej {
- Lock lock = new ReentrantLock();
- Condition c = lock.newCondition();
- int[] nacija = new int[BROJ_NACIJA];
- public Muzej() {
- for (int i = 0; i < nacija.length; i++) {
- nacija[i] = 0;
- }
- }
- public void ulaz(int i) {
- lock.lock();
- try {
- while (nacija[(i+1) % nacija.length] > 0 || nacija[(i+2) % nacija.length] > 0) {
- c.awaitUninterruptibly();
- }
- nacija[i]++;
- } finally { lock.unlock(); }
- }
- public void izlaz(int i) {
- lock.lock();
- try {
- nacija[i]--;
- if (nacija[i] == 0) {
- c.signalAll();
- }
- } finally { lock.unlock(); }
- }
- }
- 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();
- m.ulaz(0);
- obilazi();
- m.izlaz(0);
- }
- }
- private class Nemac extends UtilThread {
- public Nemac(int id) {
- super("\u041d\u0435\u043c\u0430\u0446 " + id, BOJA_NEMAC, odmaraNemac);
- }
- @Override
- protected void step() {
- odmara();
- m.ulaz(1);
- obilazi();
- m.izlaz(1);
- }
- }
- 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();
- m.ulaz(2);
- obilazi();
- m.izlaz(2);
- }
- }
- // 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 LouvreLock();
- }
- // 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 LouvreLock() {
- // 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