Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
4
Indexable
Never
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main
{
    public static void main (String[] args) {
        int n, m;
        Random r = new Random();
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter number of processes(at least 3): ");
        n = scanner.nextInt();
        while (n<3) {
            System.out.println("please enter a number with value of 3 or above:");
            n = scanner.nextInt();
        }
        ArrayList<CheckThread> arrayList = new ArrayList<>();
        System.out.println("How many rounds? ");
        m = scanner.nextInt();
        Monitor mon = new Monitor(n,m);
        for (int i = 0; i<n ; i++)
        {
            arrayList.add(new CheckThread(randNumbers(r),mon,arrayList,i));
        }

        printArray(arrayList);
        for (int i = 0; i<n ; i++)
        {
            new Thread(arrayList.get(i), Integer.toString(i)).start();
        }
        printArray(arrayList);

    }

    public static int randNumbers(Random random) // this method generates random numbers and insert them to the array
    {
        return random.nextInt(100) +1;
    }

    public static void printArray(ArrayList<CheckThread> arrayList)
    {
        for (int i = 0; i<arrayList.size(); i++)
        {
            if (i == arrayList.size() -1)
                System.out.print(arrayList.get(i).getX());
            else
                System.out.print(arrayList.get(i).getX() + ", ");
        }
        System.out.print("\n");
    }

}