Created
April 3, 2025 04:36
-
-
Save rugi/66b99b5a2e223206c212fff8a099541b to your computer and use it in GitHub Desktop.
Clase para probar creación de Threads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demorugi01; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.Scanner; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author RuGI | |
*/ | |
public class Hilos { | |
public static void main(String[] args) throws InterruptedException { | |
if (args.length < 3) { | |
System.out.println("Debes indicar 3 parametros enteros de entrada."); | |
System.out.println("Ejemplo:"); | |
System.out.println(" java Hilos 20 1 20"); | |
System.out.println(" Donde:"); | |
System.out.println(" El 1er valor es el (t) de espera antes crear Threads"); | |
System.out.println(" El 2o valor es el (n) de Threads a crear."); | |
System.out.println(" El 3er valor es el (t) de vida de cada Thread."); | |
System.out.println("En el ejemplo anterior entonces se define:"); | |
System.out.println("Espera 20s, empieza a crear 1 thread. Cada uno dura 20 s."); | |
System.out.println("Los threads terminaran. El proceso continuara"); | |
System.out.println("Para terminar pulsa la letra x seguida de ENTER."); | |
return; | |
} | |
//Aca todo OK. | |
int tiempoInicialDeEspera = Integer.parseInt(args[0]); | |
int hilosPorCrear = Integer.parseInt(args[1]); | |
double tiempoDeVidaDeCadaThread = Double.parseDouble(args[2]); | |
System.out.println(new StringBuilder().append("Tienes ") | |
.append(tiempoInicialDeEspera) | |
.append(" para abrir JConsole y seleccionar este proceso.").append("\n") | |
.append("Se crearan: ").append(hilosPorCrear).append(" threads y cada uno durara ") | |
.append(tiempoDeVidaDeCadaThread) | |
.append(" segundos.")); | |
//Descansa el main thread | |
System.out.println("Inician " + tiempoInicialDeEspera + " segundos de espera. Main Thread."); | |
Thread.sleep(tiempoInicialDeEspera * 1000L); | |
System.out.println("Inicia la creacion de threads desde el main thread.."); | |
for (int i = 0; i < hilosPorCrear; i++) { | |
RuGIThread f = new RuGIThread(i, tiempoDeVidaDeCadaThread); | |
f.start(); | |
} | |
// seguiremos vivos hasta que teclees x seguido de ENTER | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Teclea x para terminar el proceso: "); | |
do { | |
String line = sc.nextLine(); | |
if (line.trim().equals("x")) { | |
sc.close(); | |
break; | |
} | |
} while (true); | |
}//main | |
//Clase interna | |
static class RuGIThread extends Thread { | |
private final double tiempoDeEspera; | |
public RuGIThread(final int i, final double tiempoDeEspera) { | |
super(); | |
this.setName("Thread-" + i); | |
this.tiempoDeEspera = tiempoDeEspera; | |
} | |
public void run() { | |
System.out.println("Iniciando " + this.getName()); | |
try { | |
//una operacion sencilla para solicitar recursos. | |
double dato = Math.sqrt(tiempoDeEspera); | |
String tmpdir = Files.createTempDirectory("prefix_").toFile().getAbsolutePath(); | |
String tmpDirsLocation = System.getProperty("java.io.tmpdir"); | |
System.out.println("Se creo un archivo temporal: " + tmpdir); | |
Thread.sleep((long)tiempoDeEspera * 1000L); | |
} catch (InterruptedException ex) { | |
Logger.getLogger(Hilos.class.getName()).log(Level.SEVERE, null, ex); | |
} catch (IOException ex) { | |
Logger.getLogger(Hilos.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
System.out.println("Terminando " + this.getName()); | |
} | |
} | |
// Fin de clase interna | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment