Advertisement
salercode

Lab2OsZad3

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