是否有幫助:?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
import time
import random
NUMBER_OF_THREADS=20
totalPoints = []
def workThread(i):
global totalPoints
time.sleep(random.randint(0, 5))
totalPoints.append((i, random.randint(0, 255)))
threads = []
for i in range(NUMBER_OF_THREADS):
t = threading.Thread(target=workThread, args=(i,))
t.start()
threads.append(t)
for t in threads:
t.join()
print totalPoints
它始終打印是這樣的:
[(1, 126), (10, 169), (11, 154), (0, 214), (9, 243), (12, 13), (15, 152), (6, 24), (17, 238), (13, 28), (19, 78), (16, 130), (2, 110), (3, 186), (8, 55), (14, 70), (5, 35), (4, 39), (7, 11), (18, 14)]
或本
[(2, 132), (3, 53), (4, 15), (6, 84), (8, 223), (12, 39), (14, 220), (0, 128), (9, 244), (13, 80), (19, 99), (7, 184), (11, 232), (17, 191), (18, 207), (1, 177), (5, 186), (16, 63), (15, 179), (10, 143)]