Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vaterpolisti;
- import java.awt.Color;
- import java.util.concurrent.Semaphore;
- //import os.SimulationThread;
- import os.simulation.SimulationThread;
- import os.simulation.containers.*;
- //import os.simulation.display.*;
- import os.simulation.frame.*;
- public class VaterpoloSem {
- //Object lock = new Object();
- Semaphore crveni = new Semaphore(0);
- Semaphore plavi = new Semaphore(0);
- Semaphore beli = new Semaphore(0);
- Semaphore mutex = new Semaphore (1);
- int hoceCrveni, hocePlavi, hoceBeli = 0;
- private class Crveni extends UtilThread
- {
- public Crveni(int id)
- {
- super("\u0426\u0440\u0432\u0435\u043D\u0438 " + id, BOJA_CRVENI, odmaraCrveni);
- }
- @Override
- protected void step()
- {
- odmara();
- mutex.acquireUninterruptibly();
- while((hocePlavi > 0) || (hoceBeli > 0))
- {
- mutex.release();
- crveni.acquireUninterruptibly();
- mutex.acquireUninterruptibly();
- }
- ++hoceCrveni;
- mutex.release();
- trenira();
- mutex.acquireUninterruptibly();
- if (--hoceCrveni == 0)
- {
- beli.release(beli.getQueueLength());
- plavi.release(plavi.getQueueLength());
- }
- mutex.release();
- }
- }
- private class Beli extends UtilThread
- {
- public Beli(int id)
- {
- super("\u0411\u0435\u043B\u0438 " + id, BOJA_BELI, odmaraBeli);
- }
- @Override
- protected void step()
- {
- odmara();
- mutex.acquireUninterruptibly();
- while((hoceBeli > 0) || (hocePlavi > 0) || (hoceCrveni > 0))
- {
- mutex.release();
- beli.acquireUninterruptibly();
- mutex.acquireUninterruptibly();
- }
- ++hoceBeli;
- mutex.release();
- trenira();
- mutex.acquireUninterruptibly();
- if (--hoceBeli == 0)
- {
- plavi.release(plavi.getQueueLength());
- beli.release(beli.getQueueLength());
- crveni.release(crveni.getQueueLength());
- }
- mutex.release();
- }
- }
- private class Plavi extends UtilThread
- {
- public Plavi(int id)
- {
- super("\u041F\u043B\u0430\u0432\u0438 " + id, BOJA_PLAVI, odmaraPlavi);
- }
- @Override
- protected void step()
- {
- odmara();
- mutex.acquireUninterruptibly();
- while((hoceBeli > 0) || (hoceCrveni > 0))
- {
- mutex.release();
- plavi.acquireUninterruptibly();
- mutex.acquireUninterruptibly();
- }
- ++hocePlavi;
- mutex.release();
- trenira();
- mutex.acquireUninterruptibly();
- if (--hocePlavi == 0)
- {
- crveni.release(crveni.getQueueLength());
- beli.release(beli.getQueueLength());
- }
- mutex.release();
- }
- }
- // Parametri simulacije
- public static final int DUZINA_TRENIRANJA = 3000;
- public static final int DUZINA_ODMARANJA = 5000;
- public static final int BROJ_IGRACA = 7;
- // Boje
- public static final Color BOJA_PLAVI = new Color(192, 192, 255);
- public static final Color BOJA_CRVENI = new Color(255, 192, 192);
- public static final Color BOJA_BELI = new Color(255, 255, 255);
- public static final Color BOJA_KLUPE_PLAVI = new Color(0, 0, 128);
- public static final Color BOJA_KLUPE_CRVENI = new Color(128, 0, 0);
- public static final Color BOJA_KLUPE_BELI = new Color(255, 255, 255);
- public static final Color BOJA_TERENA = null;
- // Stringovi
- public static final String TEXT_TRENIRA = "\u0422\u0440\u0435\u043D\u0438\u0440\u0430";
- public static final String TEXT_TRENIRAO = "\u0417\u0430\u0432\u0440\u0448\u0438\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_KLUPA_PLAVI = "\u0421\u0432\u043B\u0430\u0447\u0438\u043E\u043D\u0438\u0446\u0430";
- public static final String TEXT_KLUPA_CRVENI = "\u0421\u0432\u043B\u0430\u0447\u0438\u043E\u043D\u0438\u0446\u0430";
- public static final String TEXT_KLUPA_BELI = "\u0421\u0432\u043B\u0430\u0447\u0438\u043E\u043D\u0438\u0446\u0430";
- public static final String TEXT_BAZEN = "\u0411\u0430\u0437\u0435\u043D";
- public class UtilThread extends SimulationThread
- {
- private ThreadContainer odmara;
- protected UtilThread(String name, Color color, ThreadContainer klupa)
- {
- super(name, color);
- odmara = klupa;
- }
- @Override
- protected void run()
- {
- while (!isStopRequested())
- {
- step();
- }
- }
- protected void odmara()
- {
- setContainer(odmara);
- setText(TEXT_ODMARA);
- work(DUZINA_ODMARANJA, 2 * DUZINA_ODMARANJA);
- setText(TEXT_ODMORIO);
- }
- protected void trenira()
- {
- setContainer(trenira);
- setText(TEXT_TRENIRA);
- work(DUZINA_TRENIRANJA, 2 * DUZINA_TRENIRANJA);
- setText(TEXT_TRENIRAO);
- }
- }
- // Glavni program
- public static void main(String [] a)
- {
- new VaterpoloSem();
- }
- // Stanja
- private ThreadContainer odmaraPlavi;
- private ThreadContainer odmaraCrveni;
- private ThreadContainer odmaraBeli;
- private ThreadContainer trenira;
- public VaterpoloSem()
- {
- // Create states
- trenira = new ThreadContainerBox(TEXT_BAZEN, BOJA_TERENA);
- odmaraPlavi = new ThreadContainerBox(TEXT_KLUPA_PLAVI, BOJA_KLUPE_PLAVI);
- odmaraCrveni = new ThreadContainerBox(TEXT_KLUPA_CRVENI, BOJA_KLUPE_CRVENI);
- odmaraBeli = new ThreadContainerBox(TEXT_KLUPA_BELI, BOJA_KLUPE_BELI);
- // Create frame
- StateRow klupe = new StateRow(odmaraCrveni, odmaraBeli, odmaraPlavi);
- SimulationFrame frame = SimulationFrame.create("Timovi", new StateColumn(klupe, trenira));
- //SimulationFrame frame2 = SimulationFrame.create
- frame.display();
- // Create threads
- for (int i = 1; i <= BROJ_IGRACA; i++)
- {
- new Plavi(i).start();
- new Crveni(i).start();
- new Beli(i).start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement