Advertisement
MrSnoopy

Java- through class multithreading and syncing

May 6th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. /* Singleton Kontejner */
  2.  
  3.  
  4.  
  5. import javax.management.monitor.CounterMonitor;
  6. import javax.management.monitor.Monitor;
  7.  
  8. /**
  9.  *
  10.  * @author xxxvodnikxxx
  11.  */
  12. public class singletonKontejner {
  13.  
  14.     //uchovávající instance pro singleton
  15.     private static singletonKontejner instance;
  16.  
  17.     //proměnné pro nastavení
  18.     private int x;
  19.     private int y;
  20.     private int z;
  21.  
  22.     //požadované hodnoty proměnných
  23.     private final int constX = 10;
  24.     private final int constY = 20;
  25.     private final int constZ = 30;
  26.  
  27.     //sync monitory
  28.     private final Monitor monX = new CounterMonitor();
  29.     private final Monitor monY = new CounterMonitor();
  30.     private final Monitor monZ = new CounterMonitor();
  31.  
  32.     //Vytvorime soukromy konstruktor
  33.     private singletonKontejner() {
  34.     }
  35.  
  36.     //Metoda pro vytvoreni objektu jedinacek
  37.     public static singletonKontejner getInstance() {
  38.         //Je-li promenna instance null, tak se vytvori objekt
  39.         if (instance == null) {
  40.             instance = new singletonKontejner();
  41.         }
  42.         //Vratime jedinacka
  43.         return instance;
  44.     }
  45.  
  46.     //metoda, která zamkne monitor - wait()
  47.     public synchronized void lockMonitor(Monitor mon) throws InterruptedException {
  48.         mon.wait();
  49.     }
  50.  
  51.     // metoda pro uvolnění monitoru- notify()
  52.     public synchronized void notifyMonitor(Monitor mon) {
  53.         mon.notify();
  54.     }
  55.  
  56.     public synchronized int spocitejVypis() throws InterruptedException {
  57.  
  58.         if (this.x == 0) {
  59.             try {
  60.                 this.lockMonitor(monX);
  61.             } catch (InterruptedException e) {
  62.                 System.out.println("Chyba wait monX");
  63.             }
  64.  
  65.         }
  66.         if (this.y == 0) {
  67.             try {
  68.                 this.lockMonitor(monY);
  69.             } catch (InterruptedException e) {
  70.                 System.out.println("Chyba wait monY");
  71.             }
  72.  
  73.         }
  74.         if (this.z == 0) {
  75.             try {
  76.                 this.lockMonitor(monZ);
  77.             } catch (InterruptedException e) {
  78.                 System.out.println("Chyba wait monZ");
  79.             }
  80.  
  81.         }
  82.  
  83.         int vysledek = 2 * this.x + 3 * this.y + 4 * this.z;
  84.         System.out.println(this.toString() + " vysledek: " + vysledek);
  85.         return vysledek;
  86.     }
  87.  
  88.     //gettery , settery
  89.     public Monitor getMonX() {
  90.         return monX;
  91.     }
  92.  
  93.     public Monitor getMonY() {
  94.         return monY;
  95.     }
  96.  
  97.     public Monitor getMonZ() {
  98.         return monZ;
  99.     }
  100.  
  101.     public int getConstX() {
  102.         return constX;
  103.     }
  104.  
  105.     public int getConstY() {
  106.         return constY;
  107.     }
  108.  
  109.     public int getConstZ() {
  110.         return constZ;
  111.     }
  112.  
  113.     public int getX() {
  114.         return x;
  115.     }
  116.  
  117.     public void setX(int x) {
  118.         this.x = x;
  119.     }
  120.  
  121.     public int getY() {
  122.         return y;
  123.     }
  124.  
  125.     public void setY(int y) {
  126.         this.y = y;
  127.     }
  128.  
  129.     public int getZ() {
  130.         return z;
  131.     }
  132.  
  133.     public void setZ(int z) {
  134.         this.z = z;
  135.     }
  136.  
  137.     @Override
  138.     public String toString() {
  139.         return "Cisla={" + x + ";" + y + ";" + z + "}";
  140.     }
  141.  
  142. }
  143.  
  144.  
  145.  
  146. /*********************************************************************************************************************/
  147.  
  148. /* zapisovací třída na 'X' - boudcí vlákno X*/
  149.  
  150. /*
  151.  * To change this license header, choose License Headers in Project Properties.
  152.  * To change this template file, choose Tools | Templates
  153.  * and open the template in the editor.
  154.  */
  155.  
  156. /**
  157.  *
  158.  * @author xxxvodnikxxx
  159.  */
  160. public class vlaknoPocX extends Thread {
  161.  
  162.     @Override
  163.     public synchronized void run() {
  164.  
  165.         //natazeni instance kontejneru
  166.         singletonKontejner objekt = singletonKontejner.getInstance();
  167.         //zapis
  168.         objekt.setX(objekt.getConstX());
  169.         //notifikace pro monitor pokud by byl zamčený
  170.         objekt.notifyMonitor(objekt.getMonX());
  171.  
  172.     }
  173.  
  174. }
  175.  
  176.  
  177.  
  178. /*********************************************************************************************************************/
  179.  
  180. /* zapisovací třída Y - budoucí vlákno číslo 2*/
  181.  
  182.  
  183. /**
  184.  *
  185.  * @author xxxvodnikxxx
  186.  */
  187. public class vlaknoPocY extends Thread {
  188.  
  189.     @Override
  190.     public synchronized void run() {
  191.  
  192.         singletonKontejner objekt = singletonKontejner.getInstance();
  193.         objekt.setY(objekt.getConstY());
  194.         objekt.notifyMonitor(objekt.getMonY());
  195.  
  196.     }
  197.  
  198. }
  199.  
  200.  
  201.  
  202. /*********************************************************************************************************************/
  203.  
  204.  
  205. /* třída pro zapsaní Z  - budoucí vlákno číslo 3*/
  206.  
  207. /*
  208.  * To change this license header, choose License Headers in Project Properties.
  209.  * To change this template file, choose Tools | Templates
  210.  * and open the template in the editor.
  211.  */
  212.  
  213. /**
  214.  *
  215.  * @author xxxvodnikxxx
  216.  */
  217. public class vlaknoPocZ extends Thread {
  218.  
  219.     @Override
  220.     public synchronized void run() {
  221.         singletonKontejner objekt = singletonKontejner.getInstance();
  222.         objekt.setZ(objekt.getConstZ());
  223.         objekt.notifyMonitor(objekt.getMonZ());
  224.     }
  225.  
  226. }
  227.  
  228.  
  229. /*********************************************************************************************************************/
  230.  
  231.  
  232. /* třída main*/
  233.  
  234.  
  235.  
  236. /**
  237.  *
  238.  * @author xxxvodnikxxx
  239.  */
  240. public class Main {
  241.  
  242.     public static void main(String[] args) throws InterruptedException {
  243.  
  244.         //vytvoření vláken
  245.         Thread vlaknoX = new vlaknoPocX();
  246.         Thread vlaknoY = new vlaknoPocY();
  247.         Thread vlaknoZ = new vlaknoPocZ();
  248.        
  249.         //spuštění vláken
  250.         vlaknoX.start();
  251.         vlaknoY.start();
  252.         vlaknoZ.start();
  253.  
  254.         // natažení instance singleton- kontejneru, pro práci (výpočet)
  255.         singletonKontejner objekt = singletonKontejner.getInstance();
  256.        
  257.         try {
  258.             objekt.spocitejVypis();
  259.         } catch (InterruptedException e) {
  260.             System.out.println("Chyba vypoctu main");
  261.         }
  262.  
  263.  
  264.     }
  265.  
  266. }
  267.  
  268.  
  269. /*********************************************************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement