#!/usr/bin/python3
import threading
import time
class myThread (threading.Thread):
def __init__(self, name, delay, list):
threading.Thread.__init__(self)
# self.threadID = threadID
self.name = name
self.delay = delay
self.list = list
def run(self):
print ("开始线程:" + self.name)
print_time(self.name, self.delay, self.list)
print ("退出线程:" + self.name)
def print_time(threadName, delay, list):
for i in list:
print("%s: %s" % (threadName,i))
time.sleep(delay)
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
x = len(a) // 5
count = 1
list_total = []
for i in range(0, len(a), x):
if count < 5:
list_total.append(a[i:i + x])
count += 1
else:
print(a[i:len(a) - 1])
list_total.append(a[i:len(a)])
break
# 创建新线程
thread1 = myThread("Thread-1", 1,list_total[0])
thread2 = myThread("Thread-2", 1,list_total[1])
thread3 = myThread("Thread-3", 1,list_total[2])
thread4 = myThread("Thread-4", 1,list_total[3])
thread5 = myThread("Thread-5", 1,list_total[4])
# 开启新线程
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
print ("退出主线程")
对列表进行分段操作,指定拆分成固定数量的多个列表。



