Crazy

Threads - Задача 3

Mar 13th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import java.util.HashSet;
  2. import java.util.Scanner;
  3. import java.util.concurrent.Semaphore;
  4.  
  5. public class Naizmenicno {
  6.  
  7.     public static int NUM_RUNS = 1000;
  8.  
  9.     int f1count;
  10.     int f2count;
  11.     int maxDifference = 0;
  12.  
  13.     /**
  14.      * Metod koj treba da gi inicijalizira vrednostite na semaforite i
  15.      * ostanatite promenlivi za sinhronizacija.
  16.      *
  17.      */
  18.  
  19.     Semaphore s1;
  20.     Semaphore s2;
  21.  
  22.     Object sync;
  23.  
  24.  
  25.  
  26.     public void init(int count) {
  27.         //da se implementira
  28.         maxDifference=count;
  29.         s1=new Semaphore(maxDifference);
  30.         s2= new Semaphore(12);
  31.         sync= new Object();
  32.  
  33.     }
  34.  
  35.     class F1Thread extends Thread {
  36.  
  37.         public void executeF1() throws InterruptedException {
  38.             //da se implementira
  39.  
  40.             s1.acquire();
  41.  
  42.             synchronized (sync){
  43.                 f1();
  44.             }
  45.  
  46.             s2.release();
  47.         }
  48.  
  49.         @Override
  50.         public void run() {
  51.             try {
  52.                 executeF1();
  53.             } catch (Exception e) {
  54.                 e.printStackTrace();
  55.             }
  56.         }
  57.     }
  58.  
  59.     class F2Thread extends Thread {
  60.  
  61.         public void executeF2() throws InterruptedException {
  62.             //da se implementira
  63.             s2.acquire();
  64.  
  65.             synchronized (sync){
  66.                 f2();
  67.             }
  68.  
  69.             s1.release();
  70.         }
  71.  
  72.         @Override
  73.         public void run() {
  74.             try {
  75.                 executeF2();
  76.             } catch (Exception e) {
  77.                 e.printStackTrace();
  78.             }
  79.         }
  80.     }
  81.  
  82.     public void f1() {
  83.         System.out.println("f1()");
  84.         f1count++;
  85.         if (f1count - f2count > maxDifference) {
  86.             maxDifference = f1count - f2count;
  87.         }
  88.     }
  89.  
  90.     public void f2() {
  91.         System.out.println("f2()");
  92.         f2count++;
  93.  
  94.         if (f1count - f2count > maxDifference) {
  95.             maxDifference = f1count - f2count;
  96.         }
  97.     }
  98.  
  99.     public static void main(String[] args) {
  100.         try {
  101.             Naizmenicno environment = new Naizmenicno();
  102.             environment.start();
  103.         } catch (Exception ex) {
  104.             ex.printStackTrace();
  105.         }
  106.     }
  107.  
  108.     public void start() throws Exception {
  109.  
  110.         System.out.println("Vnesete za kolku poveke sakate da se izvrsi f1()");
  111.         Scanner s = new Scanner(System.in);
  112.         int n = s.nextInt();
  113.         init(n);
  114.  
  115.         HashSet<Thread> threads = new HashSet<>();
  116.         for (int i = 0; i < NUM_RUNS; i++) {
  117.             F1Thread f1 = new F1Thread();
  118.             F2Thread f2 = new F2Thread();
  119.             threads.add(f1);
  120.             threads.add(f2);
  121.         }
  122.  
  123.         for (Thread t : threads) {
  124.             t.start();
  125.         }
  126.  
  127.         for (Thread t : threads) {
  128.             t.join();
  129.         }
  130.         System.out.println("F1count: " + f1count);
  131.         System.out.println("F2count: " + f2count);
  132.         System.out.println("maxDifference: " + maxDifference);
  133.         System.out.println("Status: " + (maxDifference <= n));
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment