Multithreading thread in Java

0

I have here an exercise called Parking. I understand it slightly, but there is one thing that is not clear to me, and I refer to the parameter that has Semaphore sem = new Semaphore(este_parametro_me_refiero) . I appreciate the help! I leave the code:

package ejercicio6_Parking;

import java.util.concurrent.Semaphore;

public class Coche implements Runnable{

    private Semaphore sem;
    private int numeroCoche;

    public Coche(int numeroCoche, Semaphore sem) {
        this.numeroCoche = numeroCoche;
        this.sem = sem;
    }

    @Override
    public void run() {
        System.out.println("Arranca el coche " + numeroCoche);
        while(true) {
            //el coche va por la ciudad entre 1 y 30s e intenta entrar al parking
            try {
                Thread.sleep( (long) (Math.random()*30000) );
                System.out.println("El coche " + numeroCoche + " llega al parking e intenta entrar.");
                sem.acquire();
                    System.out.println("El coche " + numeroCoche + " entra.");
                    Thread.sleep( (long) (Math.random()*30000));
                    System.out.println("El coche " + numeroCoche + " sale.");
                    System.out.println(sem.availablePermits() + " plazas libres.");
                    System.out.println(sem.getQueueLength() + " coches en la cola.");
                sem.release();
            }catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}


package ejercicio6_Parking;

import java.util.concurrent.Semaphore;

public class Principal {

    public static void main(String[] args) {

        Semaphore sem = new Semaphore(0);
        for(int i=0; i<40; i++) {
            Coche c = new Coche(i+1, sem);
            Thread th = new Thread(c);
            th.start();
        }

    }

}
    
asked by Sergio AG 04.03.2018 в 02:28
source

2 answers

1

To start with, in java a semaphore what it does is allow us in a simple way to give permissions to the threads to access the resources.

The parameter that you pass to the object Semaphore is the number of threads that can simultaneously access a resource. For example, imagine that I want to access a file but I only want that they can enter as much as 4 threads simultaneously. So when creating the object Semaphore should be such that

Semaphore semaphore = new Semaphore(4);

The methods acquire and release are methods to indicate when a thread is in process and when it has finished to allow the rest of threads (if required) to access said resource.

    
answered by 04.03.2018 / 17:56
source
1

The initialization parameter of that class, what it indicates is the number of times it will allow access in this case to the parking (called without blocking to acquire ), if you place 2, let call the method acquire 2 times, the which will allow to pass to 2 cars, once those 2 cars have occupied the seats, the next calls will block the threads and wait for the so-called release of the threads that occupied the parking spaces, which we say will release the parking spaces. / p>

In your main method, you have the for that generates several instances of your Car class, which contain the run method, and each car you pass the same instance of sem

Semaphore sem = new Semaphore(0);
for(int i=0; i<40; i++) {
    Coche c = new Coche(i+1, sem);
    Thread th = new Thread(c);
    th.start();
}

within the run method, in your class Coche ,

sem.acquire();
    System.out.println("El coche " + numeroCoche + " entra.");
    Thread.sleep( (long) (Math.random()*30000));
    System.out.println("El coche " + numeroCoche + " sale.");
    System.out.println(sem.availablePermits() + " plazas libres.");
    System.out.println(sem.getQueueLength() + " coches en la cola.");
sem.release();

when each instance manages to call the instruction sem.acquire(); , two things can happen, get a blocking call (that the traffic light does not have "space" to allow that call, increasing a counter so as not to exceed the number% co_of% that is the initialization parameter of the class permits ) or a non-blocking call (the number of calls Semaphore has not exceeded the number of acquire ). So if I initialize permits , the first 4 threads that make the call Semaphore sem = new Semaphore(4); can continue running, continue with the instructions below acquire , the threads that arrive in 5th, 6th, ... position, will be blocked (they will have to wait for the threads that continued to run normally until they reach the so-called acquire , free spaces in the traffic light), once spaces are released in the traffic light, the execution of the threads that are waiting can continue, always when there is "room for them" (non-blocking calls).

    
answered by 04.03.2018 в 03:40