Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package lygiagratusistest1;
  7.  
  8. /**
  9.  *
  10.  * @author Linas
  11.  */
  12. public class ManoTest1 {
  13.  
  14.     public static Monitorius monitorius = new Monitorius();
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args) {
  20.  
  21.         Thread[] procesai = new Thread[5];
  22.         for (int i = 0; i < 5; i++) {
  23.  
  24.             if (i < 2) {
  25.                 procesai[i] = new Thread(new Writer(i + 1));
  26.             } else {
  27.                 procesai[i] = new Thread(new Reader(i + 1));
  28.             }
  29.         }
  30.  
  31.         for (int i = 0; i < procesai.length; i++) {
  32.             procesai[i].start();
  33.         }
  34.  
  35.     }
  36.  
  37.     public static class Monitorius {
  38.  
  39.         volatile int c, d;
  40.         int readCounter;
  41.         int printCounter;
  42.  
  43.         public Monitorius() {
  44.             c = 10;
  45.             d = 100;
  46.             readCounter = 0;
  47.             printCounter = 0;
  48.         }
  49.  
  50.         public synchronized void Write(int id) {
  51.             try {
  52.                 while (readCounter < 2 && printCounter < 15) {
  53.                     wait();
  54.                 }
  55.             } catch (InterruptedException e) {
  56.  
  57.             }
  58.             if (printCounter < 15) {
  59.                 c = c + id;
  60.                 d = d - id;
  61.                 readCounter = 0;
  62.                 notifyAll();
  63.             }
  64.         }
  65.  
  66.         public synchronized int[] Read(int id, int c1, int d1) {
  67.             try {
  68.                 while ((c == c1 && d == d1) && printCounter < 15) {
  69.                     wait();
  70.                 }
  71.             } catch (InterruptedException e) {
  72.  
  73.             }
  74.  
  75.             if (printCounter < 15) {
  76.                 System.out.println((printCounter + 1) + ")   g:" + id + "    c: " + c + "  d: " + d);
  77.                 readCounter++;
  78.                 printCounter++;
  79.  
  80.                 int[] arr = {c, d};
  81.                 notifyAll();
  82.                 return arr;
  83.             }
  84.             return null;
  85.  
  86.         }
  87.     }
  88.  
  89.     static class Writer implements Runnable {
  90.  
  91.         int id;
  92.  
  93.         Writer(int gijosNr) {
  94.             this.id = gijosNr;
  95.         }
  96.  
  97.         @Override
  98.         public void run() {
  99.             while (monitorius.printCounter < 15) {
  100.                 monitorius.Write(id);
  101.             }
  102.         }
  103.  
  104.     }
  105.  
  106.     static class Reader implements Runnable {
  107.  
  108.         int id;
  109.         int c1, d1;
  110.  
  111.         Reader(int gijosNr) {
  112.             id = gijosNr;
  113.             c1 = -1;
  114.             d1 = -1;
  115.         }
  116.  
  117.         @Override
  118.         public void run() {
  119.             while (monitorius.printCounter < 15) {
  120.                 int[] arr = monitorius.Read(id, c1, d1);
  121.                 if (arr != null) {
  122.                     c1 = arr[0];
  123.                     d1 = arr[1];
  124.                 }
  125.             }
  126.         }
  127.  
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement