Facebook
From Smelly Moth, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 208
  1. package Paradigms.List11.CommonStaticVariableResolvings;
  2.  
  3.  
  4.  
  5. public class IntCellMonitor {
  6.  
  7.     // function sets isSet on false and on the beggining checks
  8.  
  9.     // if isSet is false then stops the execution of the thread until another thread invokes the notify
  10.  
  11.     // then condition is checked one more time
  12.  
  13.     // it prevents from read the same value by two threads until first thread will call set function
  14.  
  15.     // the same history with set
  16.  
  17.     public synchronized int getN(){
  18.  
  19.         while(!isSet){
  20.  
  21.             waitForNotifying();
  22.  
  23.         }
  24.  
  25.         isSet=false;
  26.  
  27.         notifyAll();
  28.  
  29.         // Wakes up all threads that are waiting on this object's monitor
  30.  
  31.         return n;
  32.  
  33.     }
  34.  
  35.  
  36.  
  37.     // function sets isSet on true and on the beggining checks
  38.  
  39.     // if isSet is true then stops the execution of the thread until another thread invokes the notify
  40.  
  41.     // then condition is checked one more time
  42.  
  43.     // it prevents from set the same value by two threads until first thread will call get function
  44.  
  45.     // the same history with set
  46.  
  47.     public synchronized void setN(int n){
  48.  
  49.         while(isSet){
  50.  
  51.             waitForNotifying();
  52.  
  53.         }
  54.  
  55.         this.n = n;
  56.  
  57.         isSet = true;
  58.  
  59.         notifyAll();
  60.  
  61.     }
  62.  
  63.  
  64.  
  65.     // above approach protects from the replicated call of getN or setN between threads
  66.  
  67.     // it forces to call getN before setN everytime
  68.  
  69.     private void waitForNotifying(){
  70.  
  71.         try{
  72.  
  73.             wait();
  74.  
  75.             // causes the current thread to wait until
  76.  
  77.             // another thread invokes the notify/notifyAll() method for this obj
  78.  
  79.         }catch(InterruptedException e){
  80.  
  81.             e.printStackTrace();
  82.  
  83.         }
  84.  
  85.     }
  86.  
  87.  
  88.  
  89.     private boolean isSet = true;
  90.  
  91.     private int n = 0;
  92.  
  93. }
  94.  
  95.  
  96.  
  97. class CountMonitor extends Thread{
  98.  
  99.     static IntCellMonitor n = new IntCellMonitor();
  100.  
  101.  
  102.  
  103.     @Override
  104.  
  105.     public void run(){
  106.  
  107.         for (int i = 0; i < 200000; i++){
  108.  
  109.             int temp = n.getN();
  110.  
  111.             n.setN(temp+1);
  112.  
  113.         }
  114.  
  115.     }
  116.  
  117.  
  118.  
  119.     public static void main(String[] args){
  120.  
  121.         CountMonitor thread1 = new CountMonitor();
  122.  
  123.         CountMonitor thread2 = new CountMonitor();
  124.  
  125.         thread1.start();
  126.  
  127.         thread2.start();
  128.  
  129.         try{
  130.  
  131.             thread1.join();
  132.  
  133.             thread2.join();
  134.  
  135.         }catch(InterruptedException e){
  136.  
  137.             System.out.println(e.getStackTrace());
  138.  
  139.             System.out.println("Catched exception");
  140.  
  141.         }
  142.  
  143.         System.out.println("The value of n is " + n.getN());
  144.  
  145.         System.out.println("Monitor edition");
  146.  
  147.     }
  148.  
  149. }