参考链接: 【莫烦Python】Threading 学会多线程 Python
参考链接: 莫烦 多线程
参考链接: threading — 基于线程的并行
testThreading3_1.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) added_thread.start() print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果,情形1: All Done!!! T1 start T1 finished 输出结果,情形2: T1 start All Done!!! T1 finished '''
testThreading3_2.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) added_thread.start() added_thread.join() # 等待,直到线程终结。 print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T1 finished All Done!!! '''
testThreading3_3.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def job_T2(): print('T2 start!!!\n') print("T2 finished!!!\n") def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) T2 = threading.Thread( target=job_T2, name='T2_20200910' ) added_thread.start() T2.start() added_thread.join() # 等待,直到线程终结。 T2.join() print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T2 start!!! T2 finished!!! T1 finished All Done!!! '''
testThreading3_4.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def job_T2(): print('T2 start!!!\n') print("T2 finished!!!\n") def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) T2 = threading.Thread( target=job_T2, name='T2_20200910' ) added_thread.start() T2.start() added_thread.join() # 等待,直到线程终结。 print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T2 start!!! T2 finished!!! T1 finished All Done!!! '''
testThreading3_5.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def job_T2(): print('T2 start!!!\n') print("T2 finished!!!\n") def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) T2 = threading.Thread( target=job_T2, name='T2_20200910' ) added_thread.start() T2.start() # added_thread.join() # 等待,直到线程终结。 T2.join() print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T2 start!!! T2 finished!!! All Done!!! T1 finished '''
testThreading3_6.py
# 教学视频第3集 import threading # 导入必要的包 import time def thread_job(): print('T1 start\n') for i in range(10): time.sleep(0.1) print('T1 finished\n') def job_T2(): print('T2 start!!!\n') print("T2 finished!!!\n") def main(): added_thread = threading.Thread( target=thread_job, name = 'CXQ20200910SCM' ) T2 = threading.Thread( target=job_T2, name='T2_20200910' ) added_thread.start() T2.start() # added_thread.join() # 等待,直到线程终结。 # T2.join() print('All Done!!!\n') if __name__ == '__main__': main() r''' 输出结果: T1 start T2 start!!! All Done!!! T2 finished!!! T1 finished '''