# main thread and child thread import Threadingimport timedef run (n): print (' Task ', N,threading.current_thread ()) #threading. Current_ Thread () prints the current thread time.sleep (2) Print (' Task done ', n) for I in range: #循环里边全是子线程, t-1 is a child thread, not the main thread t= Threading. Thread (target=run,args= (' t-%s '%i,)) T.start () #threading. Current_thread () prints the current thread print (' The main thread is the program itself, This program has a total of 51 threads: 50 sub-threads, 1 main thread ', Threading.current_thread ()) #主线程print (' View the number of currently active threads: ', Threading.active_count ())
Results:
D:\Anaconda3\python.exe c:/users/administrator/desktop/py_work/expreicise.pytask t-0 <thread (Thread-1, started 6684) >task t-1 <thread (Thread-2, started 4680) >task t-2 <thread (Thread-3, started 7588) >task t-3 < Thread (Thread-4, started 8024) >task t-4 <thread (Thread-5, started 3220) >task t-5 <thread (Thread-6, started 7800) >task t-6 <thread (Thread-7, started 6680) >task t-7 <thread (Thread-8, started 8560) >task T-8 < Thread (Thread-9, started 8516) >task t-9 <thread (Thread-10, started 4356) >task t-10 <thread (Thread-11, Started 4240) >task t-11 <thread (Thread-12, started 7048) >task t-12 <thread (Thread-13, started 7400) >task T-13 <thread (Thread-14, started 7676) >task t-14 <thread (Thread-15, started 3764) >task t-15 <thread ( Thread-16, started 6468) >task t-16 <thread (THREAD-17, started 5680) >task t-17 <thread (Thread-18, started 8036) >task t-18 <thread (Thread-19, started 1344) >task t-19 < Thread (Thread-20, started 4688) >task t-20 <thread (Thread-21, started 8228) >task t-21 <thread (Thread-22, Started 3032) >task t-22 <thread (Thread-23, started 7336) >task t-23 <thread (Thread-24, started 6424) >task T-24 <thread (Thread-25, started 6084) >task t-25 <thread (Thread-26, started 4144) >task t-26 <thread ( Thread-27, started 4436) >task t-27 <thread (Thread-28, started 6652) >task t-28 <thread (Thread-29, started 8292) >task t-29 <thread (Thread-30, started 1436) >task t-30 <thread (Thread-31, started 5176) >task t-31 <thread (Thread-32, started 3700) >task t-32 <thread (Thread-33, started 6348) >task t-33 <thread (Thread-34 , started 6308) >task t-34 <thread (Thread-35, started 8048) >task t-35 <thread (Thread-36, started 5644) > Task t-36 <thread (Thread-37, started 908) >task t-37 <thread (Thread-38, started 2504) >task t-38 <thread ( Thread-39, started 7428) >task t-39 <thread (Thread-40, Started 8496) >task t-40 <thread (Thread-41, started 8676) >task t-41 <thread (Thread-42, started 7968) >task T-42 <thread (Thread-43, started 8628) >task t-43 <thread (Thread-44, started 3936) >task t-44 <thread ( Thread-45, started 7596) >task t-45 <thread (Thread-46, started 7100) >task t-46 <thread (Thread-47, started 7084) >task t-47 <thread (Thread-48, started 6420) >task t-48 <thread (Thread-49, started 3612) >task t-49 <thread (Thread-50, started 6840) > The main thread is the program itself, the program has a total of 51 threads: 50 sub-threads, 1 main thread <_mainthread (Mainthread, started 4312 > View the current number of active threads: 51task done t-0task do t-1task done t-3task do t-4task done t-2task do t-5task done T-7task done T -10task done T-8task do t-9task done t-6task do t-11task done t-14task do t-12task done t-13task do t-15task done T-16task done T-19task do t-22task done t-17task do t-18task done t-21task do t-20task done T-27task done T-23task D One t-26task done t-24task do t-28task done T-25taSK Done T-35task do t-34task done t-31task do t-33task done t-29task do t-30task done t-32task do t-41task done t 39task done T-37task do t-38task done t-40task do t-36task done t-46task do t-47task done T-45task done T-43task Don E t-44task done t-42task do t-49task done t-48process finished with exit code 0
The number of main thread and child threads, and the resolution