Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Singleton Kontejner */
- import javax.management.monitor.CounterMonitor;
- import javax.management.monitor.Monitor;
- /**
- *
- * @author xxxvodnikxxx
- */
- public class singletonKontejner {
- //uchovávající instance pro singleton
- private static singletonKontejner instance;
- //proměnné pro nastavení
- private int x;
- private int y;
- private int z;
- //požadované hodnoty proměnných
- private final int constX = 10;
- private final int constY = 20;
- private final int constZ = 30;
- //sync monitory
- private final Monitor monX = new CounterMonitor();
- private final Monitor monY = new CounterMonitor();
- private final Monitor monZ = new CounterMonitor();
- //Vytvorime soukromy konstruktor
- private singletonKontejner() {
- }
- //Metoda pro vytvoreni objektu jedinacek
- public static singletonKontejner getInstance() {
- //Je-li promenna instance null, tak se vytvori objekt
- if (instance == null) {
- instance = new singletonKontejner();
- }
- //Vratime jedinacka
- return instance;
- }
- //metoda, která zamkne monitor - wait()
- public synchronized void lockMonitor(Monitor mon) throws InterruptedException {
- mon.wait();
- }
- // metoda pro uvolnění monitoru- notify()
- public synchronized void notifyMonitor(Monitor mon) {
- mon.notify();
- }
- public synchronized int spocitejVypis() throws InterruptedException {
- if (this.x == 0) {
- try {
- this.lockMonitor(monX);
- } catch (InterruptedException e) {
- System.out.println("Chyba wait monX");
- }
- }
- if (this.y == 0) {
- try {
- this.lockMonitor(monY);
- } catch (InterruptedException e) {
- System.out.println("Chyba wait monY");
- }
- }
- if (this.z == 0) {
- try {
- this.lockMonitor(monZ);
- } catch (InterruptedException e) {
- System.out.println("Chyba wait monZ");
- }
- }
- int vysledek = 2 * this.x + 3 * this.y + 4 * this.z;
- System.out.println(this.toString() + " vysledek: " + vysledek);
- return vysledek;
- }
- //gettery , settery
- public Monitor getMonX() {
- return monX;
- }
- public Monitor getMonY() {
- return monY;
- }
- public Monitor getMonZ() {
- return monZ;
- }
- public int getConstX() {
- return constX;
- }
- public int getConstY() {
- return constY;
- }
- public int getConstZ() {
- return constZ;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public int getZ() {
- return z;
- }
- public void setZ(int z) {
- this.z = z;
- }
- @Override
- public String toString() {
- return "Cisla={" + x + ";" + y + ";" + z + "}";
- }
- }
- /*********************************************************************************************************************/
- /* zapisovací třída na 'X' - boudcí vlákno X*/
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author xxxvodnikxxx
- */
- public class vlaknoPocX extends Thread {
- @Override
- public synchronized void run() {
- //natazeni instance kontejneru
- singletonKontejner objekt = singletonKontejner.getInstance();
- //zapis
- objekt.setX(objekt.getConstX());
- //notifikace pro monitor pokud by byl zamčený
- objekt.notifyMonitor(objekt.getMonX());
- }
- }
- /*********************************************************************************************************************/
- /* zapisovací třída Y - budoucí vlákno číslo 2*/
- /**
- *
- * @author xxxvodnikxxx
- */
- public class vlaknoPocY extends Thread {
- @Override
- public synchronized void run() {
- singletonKontejner objekt = singletonKontejner.getInstance();
- objekt.setY(objekt.getConstY());
- objekt.notifyMonitor(objekt.getMonY());
- }
- }
- /*********************************************************************************************************************/
- /* třída pro zapsaní Z - budoucí vlákno číslo 3*/
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author xxxvodnikxxx
- */
- public class vlaknoPocZ extends Thread {
- @Override
- public synchronized void run() {
- singletonKontejner objekt = singletonKontejner.getInstance();
- objekt.setZ(objekt.getConstZ());
- objekt.notifyMonitor(objekt.getMonZ());
- }
- }
- /*********************************************************************************************************************/
- /* třída main*/
- /**
- *
- * @author xxxvodnikxxx
- */
- public class Main {
- public static void main(String[] args) throws InterruptedException {
- //vytvoření vláken
- Thread vlaknoX = new vlaknoPocX();
- Thread vlaknoY = new vlaknoPocY();
- Thread vlaknoZ = new vlaknoPocZ();
- //spuštění vláken
- vlaknoX.start();
- vlaknoY.start();
- vlaknoZ.start();
- // natažení instance singleton- kontejneru, pro práci (výpočet)
- singletonKontejner objekt = singletonKontejner.getInstance();
- try {
- objekt.spocitejVypis();
- } catch (InterruptedException e) {
- System.out.println("Chyba vypoctu main");
- }
- }
- }
- /*********************************************************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement