How to stop the Thread in Java?

0 votes
I want to properly stop the thread. How can I do this?
Jun 11, 2018 in Java by Daisy
• 8,040 points
67 views

1 answer to this question.

0 votes

Use a flag, which will indicate the thread should be terminated or not. 
When you wish to stop the thread, you just set this flag and call join() method and wait for it to finish.
Make sure that the thread is thread safe and use the getter and setter method to synchronize the variable used as flag.
 

public class IndexProcessor implements Runnable {

private static final Logger LOGGER = LoggerFactory.getLogger(IndexProcessor.class);

private volatile boolean running = true;

 public void terminate() { running = false; }@Override public void run() { while (running) { try {LOGGER.debug("Sleeping...");Thread.sleep((long) 15000);LOGGER.debug("Processing"); } catch (InterruptedException e) {LOGGER.error("Exception", e);running = false; } } } }
And in SearchEngineContextListener:
public class SearchEngineContextListener implements ServletContextListener { 
private static final Logger LOGGER = LoggerFactory.getLogger(SearchEngineContextListener.class);
private Thread thread = null;
private IndexProcessor runnable = null; 
@Override 
public void contextInitialized(ServletContextEvent event) { 
runnable = new IndexProcessor(); 
thread = new Thread(runnable);
LOGGER.debug("Starting thread: " + thread); 
thread.start();
LOGGER.debug("Background process successfully started."); }
@Override 
public void contextDestroyed(ServletContextEvent event) {
LOGGER.debug("Stopping thread: " + thread); 
if (thread != null) {
runnable.terminate(); 
thread.join();
LOGGER.debug("Thread successfully stopped."); } } }
answered Jun 11, 2018 by parth295
• 4,600 points

Related Questions In Java

0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
61 views
0 votes
2 answers

How to break the nested loop in Java?

You can use break with a label for the ...READ MORE

answered Sep 20, 2018 in Java by Sushmita
• 6,850 points
80 views
0 votes
1 answer

How to read text files from the Classpath in Java?

InputStream in = this.getClass().getClassLoader().getResourceAsStream("TextFile.txt"); InputStream in = this.getClass().getResourceAsStream("/TextFile.txt"); package ...READ MORE

answered May 8, 2018 in Java by Akrati
• 3,150 points
181 views
0 votes
1 answer

How can we add leading zeros to the number in Java?

To add zeros to a number, we ...READ MORE

answered May 25, 2018 in Java by Akrati
• 3,150 points
179 views
0 votes
1 answer

How to kill a thread in Java?

You can use a shared variable as ...READ MORE

answered Jun 6, 2018 in Java by Akrati
• 3,150 points
72 views
0 votes
1 answer

difference between wait() and sleep() in java

We can find a big difference between ...READ MORE

answered Apr 25, 2018 in Java by developer_1
• 3,280 points
57 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered Apr 30, 2018 in Java by sharth
• 3,320 points
88 views
0 votes
1 answer

Gracefully stopping a java thread

The preferable way will be to use a ...READ MORE

answered Aug 20, 2018 in Java by anto.trigg4
• 3,420 points
33 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by parth295
• 4,600 points
50 views
0 votes
2 answers

How to clear the console in Java?

import java.io.IOException; public class CLS { ...READ MORE

answered Aug 27, 2018 in Java by Sushmita
• 6,850 points
4,043 views