the_alator

Untitled

May 24th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package ua.nure.rozdaybeda.Practice5.part3;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.  * Task 3
  7.  * @author Oleg
  8.  *
  9.  */
  10. public class Part3 {
  11.  
  12.     /**
  13.      * Demonstrates work of class
  14.      * @param args Command line arguments
  15.      */
  16.     public static void main(String[] args) {
  17.         ArrayList<Thread> threads = new ArrayList<Thread>();
  18.         Counter counter = new Counter();
  19.        
  20.         for(int a = 0; a < 10; a++) {
  21.             Thread current;
  22.             threads.add(current = new Thread(() ->
  23.                 {
  24.                     for(int i = 0;i < 10; i++)
  25.                         counter.notSyncAccess();
  26.                 }
  27.             ));
  28.             current.start();
  29.         }
  30.         for(Thread t: threads) {
  31.             try {
  32.                 t.join();
  33.             } catch (InterruptedException e) {
  34.                 e.printStackTrace();
  35.             }
  36.         }
  37.         threads.clear();
  38.        
  39. //      System.out.println("NEXT################################################");
  40. //     
  41. //      for(int a = 0; a < 10; a++) {
  42. //          Thread current;
  43. //          threads.add(current = new Thread(() ->
  44. //              {
  45. //                  for(int i = 0;i < 10; i++)
  46. //                      counter.syncAccess();
  47. //              }
  48. //          ));
  49. //          current.start();
  50. //      }
  51. //      for(Thread t: threads) {
  52. //          try {
  53. //              t.join();
  54. //          } catch (InterruptedException e) {
  55. //              e.printStackTrace();
  56. //          }
  57. //      }
  58. //      threads.clear();
  59.        
  60.         System.out.println("c1 " + counter.c1 + " c2 " + counter.c2);
  61.     }
  62. }
  63.  
  64. package ua.nure.rozdaybeda.Practice5.part3;
  65.  
  66. /**
  67.  * Encapsulates two counters and provides synchronized and non-synchronized access
  68.  * @author Oleg
  69.  *
  70.  */
  71. public class Counter{
  72.     /**
  73.      * Counter 1
  74.      */
  75.     volatile int c1 = 0;
  76.     /**
  77.      * Counter 2
  78.      */
  79.     volatile int c2 = 0;
  80.  
  81.     /**Compares counters and prints result then increments first counter and waits 10 milliseconds. Finally, increments secont counter.
  82.      */
  83.     public void compareAndChange() {
  84. //      System.out.println(c1 == c2 ? "equals" : "not
  85.         c1 += 1;
  86.         try {
  87.             Thread.sleep(10);
  88.         } catch (InterruptedException e) {
  89.             e.printStackTrace();
  90.         }
  91.         c2 += 1;
  92.     }
  93.  
  94.     /**
  95.      * Provides synchronized access to {@link #compareAndChange compareAndChange} method
  96.      */
  97.     public synchronized void syncAccess(){
  98.         compareAndChange();
  99.     }
  100.     /**
  101.      * Provides non-synchronized access to {@link #compareAndChange compareAndChanged} method
  102.      */
  103.     public void notSyncAccess() {
  104.         compareAndChange();
  105.     }
  106. }
Add Comment
Please, Sign In to add comment