Desenvolva uma classe chamada CountThread
para realiar uma contagem simples de 1 até 100, fazendo uma pausa de um segundo antes de apresentar cada número. Além disso, a contagem exibirá uma String
a cada dez números impressos. A classe CountThread
deve estender a classe Thread
, ou como alternativa, implementar a interface Runnable
. Reescreva o método run()
de Thread
para que apresente os números. Use o método estático Thread.sleep()
para fazer uma pausa. Um número do tipo long
representará os milissegundos.
Sierra, Kathy. (2003). Java 2: certificação Sun para programador e desenvolvedor Java 2. 2ª edição. Rio de Janeiro: Alta Books. 442 páginas.
/**
* Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Classe responsavel pela thread que realiza a contagem de 1 a 100
*/
class CountThread extends Thread
{
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run()
{
for(int i = 1; i <= 100; i++)
{
if(i < 10)
{
System.out.print("0" + i + " ");
}
else
{
System.out.print(i + " ");
}
if((i % 10) == 0)
{
System.out.println("Hahaha");
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException exception)
{
exception.printStackTrace();
}
}
}
}
/**
* Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Classe responsavel pela execucao da Thread
*/
public class Application
{
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
new CountThread().start();
}
}