# Demonstrate multithreading , Explain the multi-threaded pair IO Intensive operation has obvious optimization
"""
1、foo1 loop 2 Time , Every time sleep Time 2 second , After execution is 4 second ,foo2 loop 5 Time , Every time sleep Time 1 second , After execution is 5 second , And it is executed alternately
2、 If it is a single thread, you need 9 More than seconds , Multithreading requires 5 More than seconds , And it is executed in sequence
3、 Only when the cycle is over , The main thread will print ‘ End of main thread ’, explain join Will block the main thread
"""
import threading
import time
def foo1():
for i in range(1, 3):
print('foo1 Function number {} perform '.format(i))
time.sleep(2)
def foo2():
for i in range(1, 6):
print('foo2 Function number {} perform '.format(i))
time.sleep(1)
if __name__ == '__main__':
start_time = time.time()
t1 = threading.Thread(target=foo1)
t2 = threading.Thread(target=foo2)
t1.start()
t2.start()
t1.join()
t2.join()
end_time = start_time - time.time()
print(' Main thread end execution time {}'.format(end_time))