python network programming thread (Branch of China Merchants Technology pen questions)

Topic somewhat forgotten, probably create four threads, the output to be almost so, (not to review thread, cool, mend)

================= RESTART: /home/cmy/python/Internet/fuxi.py =================
start thread 1: 1
start thread 1: 2
start thread 1: 3
start thread 1: 4
start thread 1: 5

Thread Start 2: 6
Start Thread 2: 7
Start Thread 2: 8
Start Thread 2: 9
Start Thread 2:10

Thread Start 3:11
start thread 3:12
start thread 3:13
start thread 3:14
start thread 3:15

Thread Start 4:16
start thread 4:17
start thread 4:18
start thread 4:19
start thread 4:20

 

Code

import threading
import time
threadlock=threading.Lock()
threads=[]
i=1
class myThread(threading.Thread):
    def __init__(self,name):
        threading.Thread.__init__(self)
        self.name=name
    def run(self):
        threadlock.acquire()
        print_out(self.name)
        threadlock.release()
def print_out(name):
    global i
    count=0
    while count<5:
        print("开始线程",name,":",i)
        count+=1
        i+=1
    print("")
thread1=myThread('1')
thread2=myThread('2')
thread3=myThread('3')
thread4=myThread('4')
thread1.start()
thread2.start()
thread3.start()
thread4.start()
threads.append(thread1)
threads.append(thread2)
threads.append(thread3)
threads.append(thread4)
for t in threads:
    t.join()

  

 

Guess you like

Origin www.cnblogs.com/chenminyu/p/11712250.html