tags: Python basic learning
import threading
import time
def run():
time.sleep(2)
print('Current thread name:', threading.current_thread().name)
time.sleep(2)
if __name__ == '__main__':
start_time = time.time()
print('Main thread:', threading.current_thread().name)
thread_list = []
for i in range(5):
t = threading.Thread(target=run)
thread_list.append(t)
for t in thread_list:
# t.setDaemon(True)
t.start()
# t.join()
print('End of main thread')
print('Use time:', time.time()-start_time)
The execution of the main thread is completed, the sub-thread will continue to execute and the program will end
When t.setDaemon(True) starts the daemon process, the main thread execution is completed, and the child thread has not yet completed, the program will exit directly
Obviously, setDaemon is False by default
When using t.join(), the main thread has been waiting for the completion of the child thread before it ends and the program exits
The role of join is reflected, the main thread task is completed, enters the blocking state, and waits for other child threads to complete before terminating, which has the effect of thread synchronization!
Foreword Thread#join() Internal callSynchronization method Thread#join(long millis)This methodsynchronized Modification, the internal call is calledObject#wait(0) Note: Object # wait (0), like Object ...
Few facts 1 python default parameters after the thread is created, regardless of whether the main thread is executed, will wait for the child thread to complete the execution before exiting, with or w...
Test code...
First briefly introduce daemon thread: Work server daemon thread-like, as long as there is no request sent by the client, has been running and remains idle, much like the background. threading module ...
Look at the setDaemon () method of the thread The above output is: We modify the code: The output of the program is: It can be seen that the setDaemon () method is to determine if the sub-thread is en...
join() Create a thread (thread name THREAD-1) using Thread, which performs the above TEST () function. IFname == ‘main': The following is the main thread, the main thread will continue to perfor...
The deadlock caused by yourself cannot be unlocked Reentrant lock Alternative methods for reentrant locks Condition variables for thread synchronization Thread synchronization queue Inter-thread commu...
Written in front Here, it is mainly because some introductions on Join () are too thin, and you are copying me, I copied you, so today simple summary. Overview JOIN () method, physical method of threa...
1. Case requirements There are three threads three threads A, B, and C, and require B, C threads must be executed after the execution of A (here is required to be completed by Join) 2. Case code 3. Jo...