Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. package lab.vtora;
  2.  
  3.  
  4. import java.util.HashSet;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. public class CountThree {
  9.  
  10.     public static int NUM_RUNS = 100;
  11.     /**
  12.      * Promenlivata koja treba da go sodrzi brojot na pojavuvanja na elementot 3
  13.      */
  14.     int count = 0;
  15.     int syncCount = 0;
  16.     /**
  17.      * TODO: definirajte gi potrebnite elementi za sinhronizacija
  18.      */
  19.     public static final Object monitor = new Object();
  20.  
  21.     public void init() {
  22.     }
  23.  
  24.     class Counter extends Thread {
  25.  
  26.         private int counter;
  27.  
  28.         public void count(int[] data) throws InterruptedException {
  29.             // da se implementira
  30.             for (int i=0; i < data.length; ++i) {
  31.                 if (data[i] == 3) {
  32.                     counter++;
  33.                 }
  34.             }
  35. //            synchronized (this) {
  36. //                int value = count;
  37. //                int newValue = value + counter;
  38. //                Thread.sleep(10);
  39. //                count = newValue;
  40. //            }
  41.             synchronized (CountThree.class) {
  42.                 int value = count;
  43.                 int newValue = value + counter;
  44.                 Thread.sleep(10);
  45.                 count = newValue;
  46.             }
  47.             synchronized (monitor) {
  48.                 int value = syncCount;
  49.                 int newValue = value + counter;
  50.                 Thread.sleep(10);
  51.                 syncCount = newValue;
  52.             }
  53.  
  54.         }
  55.         private int[] data;
  56.  
  57.         public Counter(int[] data) {
  58.             this.data = data;
  59.             this.counter = 0;
  60.         }
  61.  
  62.         @Override
  63.         public void run() {
  64.             try {
  65.                 count(data);
  66.             } catch (Exception e) {
  67.                 e.printStackTrace();
  68.             }
  69.         }
  70.     }
  71.  
  72.     public static void main(String[] args) {
  73.         try {
  74.             CountThree environment = new CountThree();
  75.             environment.start();
  76.         } catch (Exception ex) {
  77.             ex.printStackTrace();
  78.         }
  79.     }
  80.  
  81.     public void start() throws Exception {
  82.  
  83.         init();
  84.  
  85.         HashSet<Thread> threads = new HashSet<Thread>();
  86.         Scanner s = new Scanner(System.in);
  87.         int total=s.nextInt();
  88.         Random random = new Random();
  89.  
  90.         for (int i = 0; i < NUM_RUNS; i++) {
  91.             int[] data = new int[total];
  92.             for (int j = 0; j < total; j++) {
  93.                 data[j] = random.nextInt(100);
  94.             }
  95.             Counter c = new Counter(data);
  96.             threads.add(c);
  97.         }
  98.  
  99.         for (Thread t : threads) {
  100.             t.start();
  101.         }
  102.  
  103.         for (Thread t : threads) {
  104.             t.join();
  105.         }
  106.         System.out.println(count);
  107.         System.out.println(syncCount);
  108.  
  109.  
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement