《操作系统》的实验代码。

24 lines
696 B

  1. #coding=utf-8
  2. import threading
  3. def thread_fun(num):
  4. for n in range(0, int(num)):
  5. print " I come from %s, num: %s" %( threading.currentThread().getName(), n)
  6. def main(thread_num):
  7. thread_list = list();
  8. # 先创建线程对象
  9. for i in range(0, thread_num):
  10. thread_name = "thread_%s" %i
  11. thread_list.append(threading.Thread(target = thread_fun, name = thread_name, args = (20,)))
  12. # 启动所有线程
  13. for thread in thread_list:
  14. thread.start()
  15. # 主线程中等待所有子线程退出
  16. for thread in thread_list:
  17. thread.join()
  18. if __name__ == "__main__":
  19. main(3)