《操作系统》的实验代码。
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

22 行
552 B

  1. import threading
  2. import time
  3. counter = 0
  4. mutex = threading.Lock()
  5. class MyThread(threading.Thread):
  6. def __init__(self):
  7. threading.Thread.__init__(self)
  8. def run(self):
  9. global counter, mutex
  10. time.sleep(1);
  11. if mutex.acquire():
  12. counter += 1
  13. print "I am %s, set counter:%s" % (self.name, counter)
  14. mutex.release()
  15. if __name__ == "__main__":
  16. for i in range(0, 100):
  17. my_thread = MyThread()
  18. my_thread.start()