python网络编程线程(招商局金科科技笔试题)
题目有点忘了,大概是创建四个线程,要的输出结果差不多是这样,(没复习到线程,凉凉,补一下)
================= RESTART: /home/cmy/python/Internet/fuxi.py =================
开始线程 1 : 1
开始线程 1 : 2
开始线程 1 : 3
开始线程 1 : 4
开始线程 1 : 5
开始线程 2 : 6
开始线程 2 : 7
开始线程 2 : 8
开始线程 2 : 9
开始线程 2 : 10
开始线程 3 : 11
开始线程 3 : 12
开始线程 3 : 13
开始线程 3 : 14
开始线程 3 : 15
开始线程 4 : 16
开始线程 4 : 17
开始线程 4 : 18
开始线程 4 : 19
开始线程 4 : 20
代码
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() 相关推荐
ProgressiveNotes 2020-11-06
ARMOTO机器人 2020-11-06
跳舞的芒果 2020-11-03
Lorrin 2020-11-02
航帆远洋 2020-10-27
withjeffrey 2020-10-23
Dmusen 2020-10-20
小帮软件机器人 2020-10-10
89347543 2020-10-10
lifan0 2020-09-25
步入人工智能 2020-09-24
Arduino机器人 2020-09-22
小海 2020-09-21
StrongHYQ 2020-09-17
瓦力人工智能 2020-09-08
未完待续 2020-09-08