Is a running thread eligable for garbage collection if the thread object is reasigned? For example:
class A(threading.Thread)
def run():
while True:
#Do stuff
a = A()
a.start()
time.sleep(60)
a = A()
at this point, even though thread A is still doing stuff, can the interpreter destroy the original A() thread? If it does, is there a way to prevent this from happening?
解决方案
My guess is no. There's still a reference to the thread in whatever structure Python uses to keep track of things. I'll test it out, but I'd be astonished if it didn't work.
EDIT Check it out:
#!/usr/bin/env python
import threading, time
class A(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name=name
self.count=0
def run(self):
while self.count<10:
print self.name, "Running!"
time.sleep(1)
self.count+=1
a=A("first")
a.start()
time.sleep(5)
a=A("second")
a.start()
first Running!
first Running!
first Running!
first Running!
first Running!
second Running!
first Running!
second Running!
first Running!
first Running!
second Running!
first Running!
second Running!
first Running!
second Running!
second Running!
second Running!
second Running!
second Running!
second Running!