Assuming the above code is running on thread A and your recently canceled report is running on thread B, you need thread A to pause before service.generateReport(id) and wait for thread B to finish / cancel.
One way to achieve this is to use Semaphore. Assuming that only one report can work at a time, first create a semaphore object that is accessible to all threads (usually in the report search service class)
Semaphore semaphore = new Semaphore(1);
At any point in your code where you need to run the report, call the acquire() method. This method blocks until permission is available. Similarly, when report execution is completed / canceled, make sure release() called. The Release method will return the resolution and wake up another waiting thread.
semaphore.acquire();
source share