0

I have spring scheduler method. And ExecutorService

@Scheduled(fixedRate = 5000) 
    public void startSchedule() throws IOException{
threadPool.submit(() -> {
            if(.......)return;
            try {
                generate(reportTasck);
            } catch (NurException | IOException e) {
                e.printStackTrace();
            }
        });
          }

Each 5 sec start my method and if a necessary condition - start new thread with my logic. How can I stop/pause particular thread?

I have button on veb page, and if I press it I need to stop my thread.

vatsal mevada
  • 5,148
  • 7
  • 39
  • 68
user5620472
  • 2,722
  • 8
  • 44
  • 97

1 Answers1

0

There is already quite some discussion on SO regarding the stopping of threads. For a variety of reasons you should not stop or kill a thread as e.g. noted here:

How do you kill a thread in Java?

In order to allow the thread to properly cleanup its resources it should be the thread's responsibility to terminate itself by e.g. periodically checking some condition using e.g. a shared variable or via the thread's interrupt flag. See this answer for more details:

How to stop a thread created by implementing runnable interface?

Community
  • 1
  • 1
Josef
  • 321
  • 1
  • 6