Advertisement
Guest User

trabalho

a guest
May 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. /*
  2.    
  3.  */
  4. package exercicioprimos;
  5.  
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8.  
  9. public class Principal {
  10.  
  11.     public static int opcao = 2;
  12.     public static int tempo = 5;
  13.  
  14.     private static class Task implements Runnable {
  15.  
  16.         private Timer timer = new Timer();
  17.  
  18.         volatile boolean finaliza = false;
  19.  
  20.         public static long ultimoPrimo = 3;
  21.         public long contador = 1;
  22.  
  23.         @Override
  24.         public void run() {
  25.  
  26.             System.out.println("INICIANDO");
  27.  
  28.             timer.schedule(new TimerTask() {
  29.  
  30.                 @Override
  31.                 public void run() {
  32.                     finaliza = true;
  33.                 }
  34.             }, tempo * 1000);
  35.  
  36.             while (!finaliza) {
  37.  
  38.                 boolean ehPrimo = false;
  39.                     long numeral;
  40.                     synchronized (this) {
  41.                        numeral = ultimoPrimo + 2;
  42.                     }
  43.                     ehPrimo = verificaPrimo(numeral);
  44.                     if (ehPrimo) {
  45.                         System.out.println(Thread.currentThread().getName() + " achou " + (numeral));
  46.                         contador++;
  47.                         synchronized (this){
  48.                             ultimoPrimo = numeral;
  49.                         }
  50.                     }
  51.  
  52.             }
  53.             System.out.println("final do loop da thread " + Thread.currentThread().getName());
  54.             timer.cancel();
  55.         }
  56.  
  57.         private boolean verificaPrimo(long numero) {
  58.             int divisor = 2;
  59.             double raiz = Math.sqrt(numero);
  60.             while (divisor <= raiz) {
  61.                 if (numero % divisor == 0) {
  62.                     return false;
  63.                 }
  64.                 divisor++;
  65.             }
  66.             return true;
  67.         }
  68.     };
  69.  
  70.     public static void main(String[] args) throws InterruptedException {
  71.         long contadorFinal;
  72.         Task task1 = new Task();
  73.         Task task2 = new Task();
  74.         Task task3 = new Task();
  75.         Task task4 = new Task();
  76.         Thread t1 = new Thread(task1);
  77.         Thread t2 = new Thread(task2);
  78.         Thread t3 = new Thread(task3);
  79.         Thread t4 = new Thread(task4);
  80.  
  81.        
  82.        
  83.         do {
  84.  
  85.             switch (opcao) {
  86.  
  87.                 case 1:
  88.                     t1.start();
  89.                    
  90.                     Thread.sleep((tempo + 3) * 1000);
  91.                     contadorFinal = task1.contador;
  92.                     System.out.println(" Foram Encontrados " + contadorFinal + " primos!"
  93.                             + "\n O ultimo Primo Encontrado foi " + Task.ultimoPrimo);
  94.  
  95.                 case 2:
  96.                     t1.start();
  97.                     t2.start();
  98.                     t3.start();
  99.                     t4.start();
  100.                    
  101.                     Thread.sleep((tempo + 3) * 1000);
  102.                     contadorFinal = task1.contador + task2.contador + task3.contador + task4.contador;
  103.                     System.out.println(" Foram Encontrados " + contadorFinal + " primos!"
  104.                             + "\n O ultimo Primo Encontrado foi " + Task.ultimoPrimo);
  105.  
  106.                 case 3:
  107.                     t1.start();
  108.                    
  109.                     Thread.sleep((tempo + 3) * 1000);
  110.                     contadorFinal = task1.contador;
  111.                     System.out.println(" Foram Encontrados " + contadorFinal + " primos!"
  112.                             + "\n O ultimo Primo Encontrado foi " + Task.ultimoPrimo);
  113.  
  114.             }
  115.         } while (opcao != 0);
  116.  
  117.         //task.run(); sem threads
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement