Desenvolva um programa que apresente os números primos existentes entre 0 e 99999. Para cada faixa de dez mil valores, crie uma Thread
e dispare o processo para a faixa de valores correspondente.
/**
* Copyright (C) 2009/2025 - 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;
/**
* Thread para apresentar os numeros primos do intervalo de 1 a 99.999
*/
public class Primes extends Thread
{
/**
* Numero de elementos de cada thread
*/
public final static int LENGTH = 10000;
/**
* Identificador da thread
*/
private int identifier;
/**
* Construtor para inicialiar a thread
*
* @param identifier identificador da thread
*/
public Primes(int identifier)
{
this.identifier = identifier;
}
/**
* Verificar se o numero eh primo
*
* @param number numero a ser verificado
* @return true caso o numero seja primo, false caso contrario
*/
private boolean isPrime(int number)
{
for(int i = 2; i < number; i++)
{
if((number % i) == 0)
{
return false;
}
}
return true;
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run()
{
int begin = identifier * LENGTH + 1;
int end = (identifier + 1) * LENGTH;
for(int number = begin; number < end; number++)
{
if(isPrime(number))
{
System.out.println(identifier + ": " + number);
}
}
}
}
/**
* Copyright (C) 2009/2025 - 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)
{
for(int i = 0; i < 10; i++)
{
new Primes(i).start();
}
}
}