from threading import Thread
import threading
import random
import time
class MyThread(Thread):
def __init__(self):
Thread.__init__(self)
self.count = 0
def run(self):
time.sleep(1)
this_name = threading.current_thread().getName()
print("%s has begin to eat" % this_name)
while True:
self.count += 1
print("%s has eaten %s hambergurs" % (this_name, self.count))
time.sleep(random.random()*4)
th1 = MyThread()
th2 = MyThread()
th1.start()
th2.start()
del th1
print("th1 is deleted")
print(th1)
The output of the code is the following:
th1 is deleted
Traceback (most recent call last):
File "D:/Data/data_file/pycharm_project/test/test.py", line 33, in
print(th1)
NameError: name 'th1' is not defined
Thread-1 has begin to eatThread-2 has begin to eat
Thread-2 has eaten 1 hambergurs
Thread-1 has eaten 1 hambergurs
Thread-2 has eaten 2 hambergurs
Thread-1 has eaten 2 hambergurs
Thread-1 has eaten 3 hambergurs
Thread-2 has eaten 3 hambergurs
So after the following code
del th1
th1 is deleted
but Thread-1 is still running
del th1
why the above code does not kill Thread-1?