0

I have this script to run through a bunch of queries and writes the results and runtime to a txt file.

This works ok for small data sets, but for larger longer queries I get a lot of

This stopwatch is already running. messages.

Here is the relevant code

Stopwatch timer = Stopwatch.createUnstarted();


    for(String query: qrys) {
        try {
            timer.start();
            resQryES = methodforQrys(url, query);   // query result is saved in the iterable Map resQryES
            timer.stop();

            out.println("Query: " + query);
            out.println("Query execution time: " + timer);
            out.println("Query Results : " + resQryES);


        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }


    out.close();

I'm thinking perhaps its because the queries are executing in parallel , so a stopwatch is still running - so it works with smaller datasets as queries are so fast it doesn't run any in parallel.

Should I try using a different loop type where I use indexes, and create a matching map/array of timer values so none get mixed up ...

3
  • Does methodforQrys start up a new thread? Feb 16, 2017 at 16:28
  • Have you tried putting Stopwatch timer = Stopwatch.createUnstarted(); inside the for loop? Feb 16, 2017 at 16:30
  • Yes - that fixed it thanks ! , the reason I didn't try before as I believed I allready had - but I did it before with Stopwatch.createStarted();
    – Yozzer
    Feb 16, 2017 at 16:38

1 Answer 1

0

As mentioned is the comments as well. Just put this line of code inside your for loop so that it will reset for every iteration.

Stopwatch timer = Stopwatch.createUnstarted();

Your code should look like this:

for(String query: qrys) {
    Stopwatch timer = Stopwatch.createUnstarted();
    try {
        timer.start();
        resQryES = methodforQrys(url, query);   // query result is saved in the iterable Map resQryES
        timer.stop();

        out.println("Query: " + query);
        out.println("Query execution time: " + timer);
        out.println("Query Results : " + resQryES);


    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

}


out.close();
2
  • yep, thanks a lot all ... btw, just on a side note, if I wanna debug or view each thread created here, whats the best way ?
    – Yozzer
    Feb 17, 2017 at 8:38
  • @Yozzer There are no separate threads created here particularly. For debugging, you can run your code with a debugger, exacts steps of which will depend on the IDE you are using. Feb 17, 2017 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.