Skip to content

Instantly share code, notes, and snippets.

@ranlix
Created March 14, 2014 09:41
Show Gist options
  • Save ranlix/9544867 to your computer and use it in GitHub Desktop.
Save ranlix/9544867 to your computer and use it in GitHub Desktop.
初尝多线程:目标成果:打印a,b,c ——sleep(1)——打印a,b,c——sleep(1)——打印a,b,c……当前的结果还是有问题的
import threading
import time
# x = 0
def write(text):
# global x
lock.acquire()
while 1:
print text,
lock.release()
time.sleep(1)
# x += 1
break
def line(text):
write('%s\n' % text)
lock = threading.Lock()
if __name__ == '__main__':
lst = ['a', 'b', 'c']
while 1:
for i in lst:
new_thread = threading.Thread(target=line, args=(i,))
new_thread.start()
new_thread.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment