Semáforo

Utilización de un semáforo

 La clase Semaphore

Semaphore in Java

Shared.java

class Shared
{
    static int count = 0;
}

MyThreadUp.java

import java.util.concurrent.Semaphore;

class MyThreadUp extends Thread {
    Semaphore semaphore;
    String threadName;

    public MyThreadUp(Semaphore s, String threadName) {
        super(threadName);
        this.semaphore = s;
        this.threadName = threadName;
    }

    @Override
    public void run() {

        System.out.println("Starting " + threadName);
        try {
            // First, get a permit.
            System.out.println(threadName + " is waiting for a permit.");

            // acquiring the lock
            semaphore.acquire();

            System.out.println(threadName + " gets a permit.");

            // Now, accessing the shared resource.
            // other waiting threads will wait, until this thread release the lock
            for (int i = 0; i < 5; i++) {
                Shared.count ++;
                System.out.println(threadName + ": " + Shared.count);

                // Now, allowing a context switch -- if possible.
                // for others threads to execute
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }

        // Release the permit.
        System.out.println(threadName + " releases the permit.");
        semaphore.release();
    }
}

MyThreadDown.java

import java.util.concurrent.Semaphore;

class MyThreadDown extends Thread {
    Semaphore semaphore;
    String threadName;

    public MyThreadDown(Semaphore s, String threadName) {
        super(threadName);
        this.semaphore = s;
        this.threadName = threadName;
    }

    @Override
    public void run() {

        System.out.println("Starting " + threadName);
        try {
            // First, get a permit.
            System.out.println(threadName + " is waiting for a permit.");

            // acquiring the lock
            semaphore.acquire();

            System.out.println(threadName + " gets a permit.");

            // Now, accessing the shared resource.
            // other waiting threads will wait, until this thread release the lock
            for (int i = 0; i < 5; i++) {
                Shared.count --;
                System.out.println(threadName + ": " + Shared.count);

                // Now, allowing a context switch -- if possible.
                // for others threads to execute
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }

        // Release the permit.
        System.out.println(threadName + " releases the permit.");
        semaphore.release();
    }

Main.java

import java.util.concurrent.Semaphore;

public class Main {
    public static void main(String[] args) {
        // creating a Semaphore object
        // with number of permits 1
        Semaphore semaphore = new Semaphore(1);

        // creating two threads with name A and B
        // Note that thread A will increment the count
        // and thread B will decrement the count
        MyThreadUp mt1 = new MyThreadUp(semaphore, "Up");
        MyThreadDown mt2 = new MyThreadDown(semaphore, "Down");

        // stating threads A and B
        mt1.start();
        mt2.start();

        // waiting for threads A and B
        try {
            mt1.join();
            mt2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // count will always remain 0 after
        // both threads will complete their execution
        System.out.println("count: " + Shared.count);
    }
}

Código en GitHub

 

Semáforo (puntos 6.3.4 y 6.3.5)

Comunicación entre hilos

Problema de los Lectores-Escritores

Ejemplo de lectores escritores

 

Ejercicio 5 de la tarea online

Deja una respuesta