threadTest.py 489 B

123456789101112131415161718192021
  1. import threading
  2. import time
  3. def print_numbers(threadName, delay):
  4. count = 0
  5. while count < 5:
  6. time.sleep(delay)
  7. count += 1
  8. print(f"{threadName}: {time.ctime(time.time())}")
  9. try:
  10. thread1 = threading.Thread(target=print_numbers, args=("Thread-1", 2))
  11. thread2 = threading.Thread(target=print_numbers, args=("Thread-2", 4))
  12. thread1.start()
  13. thread2.start()
  14. thread1.join()
  15. thread2.join()
  16. except Exception as e:
  17. print(f"Error: {e}")