Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Tema2;
- import java.util.Scanner;
- public class LanzaHilos {
- public static void main(String[]args) throws InterruptedException{
- LanzaHilos lanzador = new LanzaHilos();
- }
- public LanzaHilos() throws InterruptedException{
- Runnable mostrarNumeros = new MostrarNumeros();
- Scanner sc = new Scanner(System.in);
- System.out.print("Introduzca numero a descomponer ");
- int n = sc.nextInt();
- System.out.print("Introduzca numero de factores ");
- int nFactores = sc.nextInt();
- Runnable factoresPrimos = new FactoresPrimos(n, nFactores);
- Thread mostrarTask = new Thread(mostrarNumeros);
- Thread factoresTask = new Thread(factoresPrimos);
- mostrarTask.start();
- factoresTask.start();
- Thread.sleep((int)(Math.random()*3000));
- mostrarTask.interrupt();
- System.out.println("Hilo 1 interrumpido: "+mostrarTask.isInterrupted());
- System.out.println("Hilo 1 vivo: "+mostrarTask.isAlive());
- Thread.sleep((int)(Math.random()*3000));
- factoresTask.interrupt();
- System.out.println("Hilo 2 vivo: "+factoresTask.isAlive());
- System.out.println("Hilo 2 interrumpido: "+factoresTask.isInterrupted());
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Tema2;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Jovifra
- */
- public class MostrarNumeros implements Runnable{
- @Override
- public void run() {
- for(int i=1; i<=100; i++){
- try {
- System.out.println(i);
- Thread.sleep(1000);
- } catch (InterruptedException ex) {
- System.out.println("INTERRUPTED EXCEPTION CUENTA 100 con nº "+i);
- if(i%2==0) {
- System.out.println("Es par así que me vuelvo");
- Thread.currentThread().interrupt();
- return;
- } else {
- System.out.println("Es impar, sigo");
- }
- }
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Tema2;
- /**
- *
- * @author Jovifra
- */
- public class FactoresPrimos implements Runnable {
- int n;
- int nFactores;
- public FactoresPrimos(int n, int nFactores) {
- this.n = n;
- this.nFactores = nFactores;
- }
- @Override
- public void run(){
- int factoresPrimos = 0;
- for (int i=1; i<=n/2; i++) {
- if (Thread.currentThread().isInterrupted()) {
- if(factoresPrimos>=nFactores){
- System.out.println("Numeros de factores alcanzados, corto");
- Thread.currentThread().interrupt();
- return;
- }
- }
- if(n%i==0 && esPrimo(i)) {
- System.out.println("Factor "+i+ " "+"\tes primo");
- ++factoresPrimos;
- }
- }
- return;
- }
- private boolean esPrimo(int n) {
- int divisores=0;
- for (int i=2; i<=n/2; i++) {
- if (n%i==0) ++divisores;
- }
- return (divisores==0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement