Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ejercicios;
- import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
- import es.urjc.etsii.code.concurrency.SimpleSemaphore;
- public class ejercicio16 {
- private static final int N_HILOS = 4;
- public static volatile int posicion = 0;
- public static volatile int pintadoLetra = 0;
- public static volatile int contador = 0;
- private volatile static SimpleSemaphore barrera = new SimpleSemaphore(0);
- private volatile static SimpleSemaphore terminado = new SimpleSemaphore(N_HILOS);
- public static void hilo() {
- enterMutex("posicion");
- String letra = obtenerLetra(posicion++);
- exitMutex("posicion");
- while (true) {
- terminado.acquire();
- print(letra);
- enterMutex();
- pintadoLetra++;
- if (pintadoLetra < N_HILOS) {
- exitMutex();
- barrera.acquire();
- enterMutex();
- contador++;
- exitMutex();
- } else {
- pintadoLetra = 0;
- exitMutex();
- println("-");
- barrera.release(N_HILOS - 1);
- }
- if (contador == N_HILOS - 1) {
- contador = 0;
- terminado.release(N_HILOS);
- }
- }
- }
- public static String obtenerLetra(int i) {
- if (i == 0) {
- return "A";
- } else if (i == 1) {
- return "B";
- } else if (i == 2) {
- return "C";
- } else if (i == 3) {
- return "D";
- }
- return null;
- }
- public static void main(String[] args) {
- createThreads(N_HILOS, "hilo");
- startThreadsAndWait();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement