Is it possible to terminate a running thread without setting/checking any flags/semaphores/etc.?
Yes it is possible to terminate a running thread but before terminating or killing thread you should think of the following cases:-
Thread holds a critical resource that must be closed properly.
When we create one thread it creates several threads that must be killed.
So for handling this problem, we have a very nice way exit_request flag so that at regular intervals each thread check if it is time for it to exit.
Let’s understand this with an example:-
import threading class StoppableThread(threading.Thread):def __init__(self): super(StoppableThread, self).__init__()self._stop_event = threading.Event() def stop(self):self._stop_event.set() def stopped(self):return self._stop_event.is_set()
self._stop_event = threading.Event()
In this above code, you should call stop()on the thread when you want it to exit, and wait for the thread to exit properly using join(). The thread should check the stop flag at regular intervals.
Certification in Digital Marketing |
Certification in Big Data Analytics |
Certification in Cloud & Devops |
Certification in Full Stack Web Development
Big Data and Data Science Master’s Course |
Data Science Architect Master’s Program |
Cloud and DevOps Architect Master’s Course |
Artificial Intelligence Engineer Master’s Course |
Microsoft Azure Certification Master’s Training
Data Science Course Online |
Big Data Hadoop Certification Training |
Machine Learning Course Online |
Artificial Intelligence Course and Training |
Salesforce Certification Training: Administrator and App Builder |
Tableau Training and Certification Course |
Python Data Science Course & Training |
Ethical Hacking Course |
Business Analyst Course |
Courses in Cyber Security
© COPYRIGHT 2011-2021 INTELLIPAAT.COM. ALL RIGHTS RESERVED.
Download Salary Trends Now !
Learn how professionals like you got up to 100% Salary Hike.