Last active
January 3, 2016 01:48
-
-
Save SergioLarios/8391087 to your computer and use it in GitHub Desktop.
This file contains 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 main; | |
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.Random; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class Run { | |
public static void main(String[] args) { | |
final int hilos = 5; | |
try { | |
String threadId = null; | |
if (args.length > 0) { | |
threadId = args[0]; | |
} | |
if (threadId == null) { | |
ExecutorService exec = Executors.newFixedThreadPool(hilos); | |
// Main | |
for (int i = 1; i <= hilos; i++) { | |
exec.execute(new Runnable() {public void run() { | |
try { | |
long inThId = Thread.currentThread().getId() % hilos + 1; | |
Process process = Runtime.getRuntime().exec("java -jar multipids.jar " + inThId); | |
InputStream is = process.getInputStream(); | |
InputStreamReader isr = new InputStreamReader(is); | |
BufferedReader br = new BufferedReader(isr); | |
String line; | |
while ((line = br.readLine()) != null) { | |
System.out.println(line); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
}}); | |
} | |
} | |
else { | |
// Sleep 1 | |
int rndSleep = rand.nextInt(MAX_SLEEP) + 1; | |
System.out.println("Thread " + threadId + " duerme " | |
+ rndSleep | |
+ " segundos antes de la sección de memória."); | |
Thread.sleep(rndSleep * 1000); | |
// Memoria | |
System.out.println("Thread " + threadId | |
+ " inicia los cálculos de memória"); | |
String str = BLANK; | |
String rnStr = BLANK; | |
for (int i = 0; i < TIMES; i++) { | |
rnStr = BLANK; | |
int newTimes = rand.nextInt(TIMES); | |
for (int x = 0; x < newTimes; x++) { | |
rnStr = rnStr + EN_ALPHABET[rand.nextInt(EN_ALPHABET.length)]; | |
} | |
str = str + str + rnStr; | |
} | |
System.out.println("Thread " + threadId | |
+ " ha acabado con un string de longitud: " | |
+ str.length()); | |
// Sleep 2 | |
rndSleep = rand.nextInt(MAX_SLEEP) + 1; | |
System.out.println("Thread " | |
+ threadId | |
+ " duerme " | |
+ rndSleep | |
+ " segundos antes de ejecutar los procesos de cálculo"); | |
Thread.sleep(rndSleep * 1000); | |
// CPU | |
int fibo = rand.nextInt(FIB_RANGE[1] - FIB_RANGE[0]) | |
+ FIB_RANGE[0]; | |
System.out.println("Thread " + threadId | |
+ " inicia la secuencia fibonacci de " + fibo); | |
long fibuResult = fibonacci(fibo); | |
System.out.println("Thread " + threadId | |
+ " termina la secuencia fibonacci de " + fibo | |
+ " con resultado " + fibuResult); | |
System.out.println("*******************************************"); | |
System.out.println("* Thread " + threadId + " terminado "); | |
System.out.println("*******************************************"); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
// Fibonacci | |
public static long fibonacci(int n) { | |
if (n == 0) | |
return 0; | |
else if (n == 1) | |
return 1; | |
else | |
return fibonacci(n - 1) + fibonacci(n - 2); | |
} | |
// Private constants | |
private static final String BLANK = ""; | |
private static final int TIMES = 20; | |
private static final int MAX_SLEEP = 5; | |
private static final int[] FIB_RANGE = { 40, 55 }; | |
private static Random rand = new Random(); | |
private static final String[] EN_ALPHABET = { "A", "B", "C", "D", "E", "F", | |
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", | |
"T", "U", "V", "W", "X", "Y", "Z" }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment