Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

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.

Arquivo Primes.java

/**
 * 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;

/**
 * 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);
      }
    }
  }
}

Arquivo Application.java

/**
 * 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)
  {
    for(int i = 0; i < 10; i++)
    {
      new Primes(i).start();
    }
  }
}

Saída

0: 1
1: 10007
1: 10009
0: 2
2: 20011
1: 10037
1: 10039
0: 3
0: 5
0: 7
...
9: 99877
9: 99881
9: 99901
9: 99907
9: 99923
9: 99929
9: 99961
9: 99971
9: 99989
9: 99991