#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May 15 20:21:58 2022

@author: ojacques

Cada thread faz uma impressão decrescente de 5 até 1
"""

from threading import Thread
import sys
import os
COUNTDOWN = 5

class Th(Thread):

    def __init__ (self, num):
        sys.stdout.write("Iniciando thread número " + str(num) + "\n")
        sys.stdout.flush()
        Thread.__init__(self)
        self.num = num
        self.countdown = COUNTDOWN

    def run(self):
        while (self.countdown):
            # chamadas a print por diversas threads simultaneamente não garantem a ordem de impressão dos caracteres. 
            if self.countdown>1:
                sys.stdout.write("Thread " + str(self.num) + " (" + str(self.countdown) + ")\n")
            else:
                sys.stdout.write("Thread " + str(self.num) + " (" + str(self.countdown) + ") TERMINADO\n")
            sys.stdout.flush()
            self.countdown -= 1
           
                
os.system('clear') or None
print("=========================================================\n")
print("Mostrando 5 threads em contagem decrescente de 5 até 1\n")

print("Para começar pressione qualquer tecla\n")

print("=========================================================\n")

input('')
for thread_number in range (5):
    thread = Th(thread_number)
    thread.start()

print('\n\n')