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,140 points
1,534 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 Parth
• 4,640 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
840 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,920 points
1,237 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,190 points
2,829 views
0 votes
2 answers

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

From Java 1.5 you can use the String.format method. ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 59,230 points
5,160 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,190 points
1,190 views
+1 vote
1 answer

Multithreading in Java with priorities

HI, @Juan, By default, a thread inherits the priority of its parent thread. ...READ MORE

answered Nov 6, 2020 in Java by Gitika
• 65,770 points
2,546 views
0 votes
1 answer

difference between wait() and sleep() in java

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

answered Apr 26, 2018 in Java by developer_1
• 3,350 points
1,212 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

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

answered May 1, 2018 in Java by sharth
• 3,370 points
1,154 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 Parth
• 4,640 points
959 views
0 votes
3 answers

How to clear the console in Java?

import java.io.IOException; public class chkClearScreen {     public static void ...READ MORE

answered Aug 26, 2020 in Java by ItsJustRaja
36,104 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP