daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Dec 12th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Tema2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class LanzaHilos {
  6.     public static void main(String[]args) throws InterruptedException{
  7.         LanzaHilos lanzador = new LanzaHilos();
  8.     }
  9.    
  10.     public LanzaHilos() throws InterruptedException{
  11.         Runnable mostrarNumeros = new MostrarNumeros();
  12.         Scanner sc = new Scanner(System.in);
  13.         System.out.print("Introduzca numero a descomponer ");
  14.         int n = sc.nextInt();
  15.         System.out.print("Introduzca numero de factores ");
  16.         int nFactores = sc.nextInt();
  17.         Runnable factoresPrimos = new FactoresPrimos(n, nFactores);
  18.        
  19.         Thread mostrarTask = new Thread(mostrarNumeros);
  20.         Thread factoresTask = new Thread(factoresPrimos);
  21.        
  22.         mostrarTask.start();
  23.         factoresTask.start();
  24.        
  25.         Thread.sleep((int)(Math.random()*3000));
  26.         mostrarTask.interrupt();
  27.         System.out.println("Hilo 1 interrumpido: "+mostrarTask.isInterrupted());
  28.         System.out.println("Hilo 1 vivo: "+mostrarTask.isAlive());
  29.        
  30.        
  31.         Thread.sleep((int)(Math.random()*3000));
  32.         factoresTask.interrupt();
  33.         System.out.println("Hilo 2 vivo: "+factoresTask.isAlive());
  34.         System.out.println("Hilo 2 interrumpido: "+factoresTask.isInterrupted());
  35.     }
  36. }
  37.  
  38. /*
  39.  * To change this license header, choose License Headers in Project Properties.
  40.  * To change this template file, choose Tools | Templates
  41.  * and open the template in the editor.
  42.  */
  43. package Tema2;
  44.  
  45. import java.util.logging.Level;
  46. import java.util.logging.Logger;
  47.  
  48. /**
  49.  *
  50.  * @author Jovifra
  51.  */
  52. public class MostrarNumeros implements Runnable{
  53.     @Override
  54.     public void run() {
  55.         for(int i=1; i<=100; i++){
  56.             try {
  57.                 System.out.println(i);
  58.                 Thread.sleep(1000);
  59.             } catch (InterruptedException ex) {
  60.                 System.out.println("INTERRUPTED EXCEPTION CUENTA 100 con nº "+i);
  61.                 if(i%2==0) {
  62.                     System.out.println("Es par así que me vuelvo");
  63.                     Thread.currentThread().interrupt();
  64.                     return;
  65.                 } else {
  66.                     System.out.println("Es impar, sigo");
  67.                 }
  68.             }
  69.         }
  70.     }
  71.    
  72. }
  73.  
  74. /*
  75.  * To change this license header, choose License Headers in Project Properties.
  76.  * To change this template file, choose Tools | Templates
  77.  * and open the template in the editor.
  78.  */
  79. package Tema2;
  80.  
  81. /**
  82.  *
  83.  * @author Jovifra
  84.  */
  85. public class FactoresPrimos implements Runnable {
  86.     int n;
  87.     int nFactores;
  88.     public FactoresPrimos(int n, int nFactores) {
  89.         this.n = n;
  90.         this.nFactores = nFactores;
  91.     }
  92.  
  93.     @Override
  94.     public void run(){
  95.         int factoresPrimos = 0;
  96.         for (int i=1; i<=n/2; i++) {
  97.             if (Thread.currentThread().isInterrupted()) {
  98.                 if(factoresPrimos>=nFactores){
  99.                     System.out.println("Numeros de factores alcanzados, corto");
  100.                     Thread.currentThread().interrupt();
  101.                     return;
  102.                 }
  103.             }
  104.             if(n%i==0 && esPrimo(i)) {
  105.                 System.out.println("Factor "+i+ " "+"\tes primo");
  106.                 ++factoresPrimos;
  107.             }
  108.         }
  109.         return;
  110.     }
  111.    
  112.     private boolean esPrimo(int n) {
  113.         int divisores=0;
  114.         for (int i=2; i<=n/2; i++) {
  115.             if (n%i==0) ++divisores;
  116.         }
  117.         return (divisores==0);
  118.     }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top