package examenTBJ.clases;
import java.util.Random;
import java.util.concurrent.Semaphore;
// Clase Reino
public class Reino {
private final Semaphore pasillo = new Semaphore(1);
private final Random r = new Random();
private int mineralTotal = 0;
private final int mineralObjetivo;
public Reino(int mineralObjetivo) {
this.mineralObjetivo = mineralObjetivo;
}
public synchronized boolean aportarMineral(int cantidad) {
mineralTotal += cantidad;
System.out.println("Aporte actual: " + mineralTotal + " / " + mineralObjetivo);
return mineralTotal < mineralObjetivo;
}
public Semaphore getPasillo() { return pasillo; }
public Random getRandom() { return r; }
public int getMineralTotal() { return mineralTotal; }
public int getMineralObjetivo() { return mineralObjetivo; }
}
// Clase Enano
class Enano extends Thread {
private final int id;
private final Reino reino;
public Enano(int id, Reino reino) {
this.id = id;
this.reino = reino;
}
@Override
public void run() {
try {
while (true) {
System.out.println("Enano " + id + " cruzando el pasillo hacia la mina...");
Thread.sleep(reino.getRandom().nextInt(1000, 5000));
reino.getPasillo().acquire();
System.out.println("Enano " + id + " ha llegado a la mina");
reino.getPasillo().release();
System.out.println("Enano " + id + " trabajando...");
Thread.sleep(reino.getRandom().nextInt(5000, 20000));
int mineral = reino.getRandom().nextInt(1, 20);
System.out.println("Enano " + id + " ha conseguido " + mineral);
reino.getPasillo().acquire();
System.out.println("Enano " + id + " cruzando hacia el exterior...");
Thread.sleep(reino.getRandom().nextInt(1000, 5000));
reino.getPasillo().release();
if (!reino.aportarMineral(mineral)) {
System.out.println("Enano " + id + " deja de trabajar.");
break;
}
}
} catch (Exception e) {}
}
}
// Clase Main
class Main {
public static void main(String[] args) {
int numeroEnanos = 5;
int mineralObjetivo = 100;
Reino reino = new Reino(mineralObjetivo);
for (int i = 1; i <= numeroEnanos; i++) {
new Enano(i, reino).start();
}
}
}
import java.io.*;
import java.util.List;
public class Procesos {
public static void ejecutarPipeline(
List comando1,
List comando2,
String redirectInput,
String redirectOutput,
String directory,
int nRepeticiones) throws IOException {
File dir = directory != null ? new File(directory) : null;
// Modo segundo plano
if (nRepeticiones == 0) {
ProcessBuilder pb1 = new ProcessBuilder(comando1);
ProcessBuilder pb2 = new ProcessBuilder(comando2);
if (dir != null) {
pb1.directory(dir);
pb2.directory(dir);
}
if (redirectInput != null) pb1.redirectInput(new File(redirectInput));
if (redirectOutput != null) pb2.redirectOutput(new File(redirectOutput));
List pipeline = ProcessBuilder.startPipeline(List.of(pb1, pb2));
System.out.println("Ejecutando en segundo plano. PID: " + pipeline.get(0).pid());
return;
}
// Repeticiones
for (int i = 1; i <= nRepeticiones; i++) {
System.out.println("\n--- EJECUCIÓN " + i + " ---");
ProcessBuilder pb1 = new ProcessBuilder(comando1);
ProcessBuilder pb2 = new ProcessBuilder(comando2);
if (dir != null) {
pb1.directory(dir);
pb2.directory(dir);
}
if (redirectInput != null) pb1.redirectInput(new File(redirectInput));
if (redirectOutput != null) pb2.redirectOutput(new File(redirectOutput));
List pipeline = ProcessBuilder.startPipeline(List.of(pb1, pb2));
Process p2 = pipeline.get(1);
if (redirectOutput == null) {
try (BufferedReader out = new BufferedReader(new InputStreamReader(p2.getInputStream()));
BufferedReader err = new BufferedReader(new InputStreamReader(p2.getErrorStream()))) {
System.out.println("Salida del comando:");
String linea;
while ((linea = out.readLine()) != null) System.out.println(linea);
System.out.println("Errores:");
while ((linea = err.readLine()) != null) System.out.println(linea);
}
}
try { p2.waitFor(); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
}
Este sitio no está asociado a ningún centro educativo oficial.
Estamos trabajando en una emocionante actualización para La Arboleda. Pronto experimentarás un diseño moderno y dinámico que revolucionará tu experiencia.