package concurrency.chapter3_thread_assist.recipe5_phaser.core;


import concurrency.chapter3_thread_assist.recipe5_phaser.task.FileSearch;

import java.util.concurrent.Phaser;

/**
 * Main class of the example
 */
public class Main {

    /**
     * Main method of the example
     *
     * @param args
     */
    public static void main(String[] args) {

        // Creates a Phaser with three participants
        Phaser phaser = new Phaser(3);

        // Creates 3 FileSearch objects. Each of them search in different directory
        FileSearch system = new FileSearch("/Users/magaoting/Documents/workspace_idea/guoxin/guoxin-cloud-builds/logs", "log", phaser);
        FileSearch apps = new FileSearch("/Users/magaoting/Documents/workspace_idea/guoxin/guoxin-cloud-statistics-platform-portal/logs", "log", phaser);
        FileSearch documents = new FileSearch("C:\\Documents And Settings", "log", phaser);

        // Creates a thread to run the system FileSearch and starts it
        Thread systemThread = new Thread(system, "System");
        systemThread.start();

        // Creates a thread to run the apps FileSearch and starts it
        Thread appsThread = new Thread(apps, "Apps");
        appsThread.start();

        // Creates a thread to run the documents  FileSearch and starts it
        Thread documentsThread = new Thread(documents, "Documents");
        documentsThread.start();


        try {
            systemThread.join();
            appsThread.join();
            documentsThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.printf("Terminated: %s\n", phaser.isTerminated());

    }

}
