Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashSet;
- //import java.util.Random;
- import java.util.Scanner;
- import java.util.concurrent.Semaphore;
- public class CountThree {
- public static int NUM_RUNS = 100;
- /**
- * Promenlivata koja treba da go sodrzi brojot na pojavuvanja na elementot 3
- */
- int count = 0;
- public static Semaphore counterThree;
- /**
- * TODO: definirajte gi potrebnite elementi za sinhronizacija
- */
- public void init() {
- counterThree = new Semaphore(1);
- }
- class Counter extends Thread {
- public void count(int[] data) throws InterruptedException {
- for(int i : data) {
- if(i == 3){
- counterThree.acquire();
- count++;
- counterThree.release();
- }
- }
- // da se implementira
- }
- private int[] data;
- public Counter(int[] data) {
- this.data = data;
- }
- @Override
- public void run() {
- try {
- count(data);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- try {
- CountThree environment = new CountThree();
- environment.start();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public void start() throws Exception {
- init();
- HashSet<Thread> threads = new HashSet<Thread>();
- Scanner s = new Scanner(System.in);
- int total=s.nextInt();
- for (int i = 0; i < NUM_RUNS; i++) {
- int[] data = new int[total];
- for (int j = 0; j < total; j++) {
- data[j] = s.nextInt();
- }
- Counter c = new Counter(data);
- threads.add(c);
- }
- for (Thread t : threads) {
- t.start();
- }
- for (Thread t : threads) {
- t.join();
- }
- System.out.println(count);
- s.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement