Exit while loop

189 views Asked by At

I was wondering how to exit a while loop properly in another class.

My "slave" app is connected with WebSocket through my "master app". This "slave app" is checking if there is new files in a directory. For the future, it will analyse these new files.

In my slave app, I've got Callback which tells me if it is started/paused/stopped...

What I would like to have, is to launch my loop method when my "master app" is started (via my callback started?), to stop my loop when my master app is stopped ... and....

My Loop Class :

public class Loop extends Thread {
    private boolean progIsFinished;

    public Loop() {
    }

    public void run() {
        try {
            waitingForFiles();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void waitingForFiles() throws Exception {
        progIsFinished = false;
        final File folder = new File("folder");

        long sleepDuration = 10000;
        ArrayList<File> newFileList;

        int counter = 0;
        while (progIsFinished != true) {
            newFileList = listLastModifiedFiles(folder, sleepDuration);
            Thread.sleep(sleepDuration);
        }
    }

    public static ArrayList<File> listLastModifiedFiles(File folder, long sleepDuration) throws Exception {
        ArrayList<File> newFileList = new ArrayList<File>();
        // return the new file
        return newFileList;
    }
}

My Main class :

private Main(){
    // read conf.xml and stores variables
    readConfig();
    if ( ServerConnected ) {
        // connect to server via URL
        connect( serverURL );
    } else
        System.out.println("No Conf or No connexion");
}

@Override
public void connectCallback() {
    System.out.println("CONNECTED");
}

@Override
public void startCallback() {
    // debug
    System.out.println("SIMULATION IN PROGRESS");
    loop = new Loop(this);
    try {
        loop.start();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void stopCallback() {
    // debug
    System.out.println("SIMULATION STOPPED");
}
@Override
public void pauseCallback() {
    // debug
    System.out.println("SIMULATION IN PAUSE - WAIT FOR RESUME");

}
public static void main(String[] args) {       
    Main main = new Main(); 
    }
}
0

There are 0 answers