码迷,mamicode.com
首页 > 编程语言 > 详细

使用多线程的方式打印 Hello World

时间:2017-11-28 21:54:48      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:imp   world   bsp   root   odi   style   nbsp   locate   roo   

#!/usr/bin/env python
#encoding:utf8

import thread

def hello():
    for i in xrange(5):
        w_lock.acquire()
        print Hello,
        h_lock.release()

def world():
    for i in xrange(5):
        h_lock.acquire()
        print World
        w_lock.release()
    lock.release()

lock = thread.allocate_lock()
lock.acquire()
h_lock = thread.allocate_lock()
w_lock = thread.allocate_lock()
h_lock.acquire()
thread.start_new_thread(hello, ())
thread.start_new_thread(world, ())

while lock.locked():
    pass
[root@localhost ~]# python 1.py
Hello World
Hello World
Hello World
Hello World
Hello World

 

 

 

 

 

 

      

使用多线程的方式打印 Hello World

标签:imp   world   bsp   root   odi   style   nbsp   locate   roo   

原文地址:http://www.cnblogs.com/pzk7788/p/7912025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!