1. 程式人生 > >使用多線程的方式打印 Hello World

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

線程 release bsp pre while localhost pri ase bin

#!/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