- 论坛徽章:
- 0
|
问题在代码的注释里,谢谢
############################ Begin Python Code ############################
import thread
import dummy_thread
import datetime
import time
NUM = 0
def addNum():
global NUM
for i in range(3000000):
NUM = NUM + 1
st = datetime.datetime.now()
for i in range(3):
#addNum()
#thread.start_new_thread(addNum, ())
dummy_thread.start_new_thread(addNum, ())
#Q1: module "thread" needs a lock to ensure sync of the global variable, module "dummy_thread" doesn't, why?
et = datetime.datetime.now()
#Q2: calling sleep() is important to run this script when using module "thread" but not to module "dummy_thread" (on win7), why?
time.sleep(0.1)
print NUM
print et - st
############################ End Python Code ############################ |
|