0% found this document useful (0 votes)
17 views5 pages

PSP - Tarea 02

The document describes a producer-consumer problem implemented in Java using threads and semaphores. It defines Producer, Consumer and Buffer classes that implement a threaded solution to the problem. A main method launches the producer and consumer threads to run concurrently, accessing a shared buffer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

PSP - Tarea 02

The document describes a producer-consumer problem implemented in Java using threads and semaphores. It defines Producer, Consumer and Buffer classes that implement a threaded solution to the problem. A main method launches the producer and consumer threads to run concurrently, accessing a shared buffer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PSP Tarea 02

Ej 1

Creamos 3 clases Consumidor, Productor, Bufer:

Bufer

import java.util.logging.Level;
import java.util.logging.Logger;
public class PSP_tarea02 {
public static void main(String[] args) {
try {PSP02_Bufer b = new PSP02_Bufer(6);
PSP02_Productor p = new PSP02_Productor(b);
PSP02_Consumidor c = new PSP02_Consumidor(b);
p.start();
Thread.sleep(3000);
c.start();
p.join();
c.join();
System.out.println("Programa terminada");
} catch (InterruptedException ex) {
Logger.getLogger(PSP_tarea02.class.getName()).log(Level.SEVERE, null, ex);}}}

Productor

import java.util.logging.Level;
import java.util.logging.Logger;

public class PSP02_Productor extends Thread {


private PSP02_Bufer bufer;
private final String words = "ñlkjhgfdsmnbvcx";
private int produced;
private final int LIMIT = 15;
public PSP02_Productor(PSP02_Bufer bufer){
this.produced = 0;
this.bufer = bufer;}
@Override
public void run(){
while(produced < LIMIT){
try {
char c = words.charAt((int)(Math.random() * words.length()));
bufer.producir(c);
produced++;
System.out.println("Depositado el caracter " + c + " del buffer");
sleep((long) (Math.random() * 4000));
} catch (InterruptedException ex) {
Logger.getLogger(PSP02_Productor.class.getName()).log(Level.SEVERE, null, ex);}}}}

Consumidor
import static java.lang.Thread.sleep;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PSP02_Consumidor extends Thread {

private PSP02_Bufer buffer;


private int consumed;
private final int LIMIT = 15;

public PSP02_Consumidor(PSP02_Bufer buffer){


this.consumed = 0;
this.buffer = buffer;}
public void run(){
while(consumed < LIMIT){
try {
char c = buffer.consumir();
consumed++;
System.out.println("Recogido el caracteres " + c + " del buffer");
sleep((long) (Math.random() * 4000));
} catch (InterruptedException ex) {
Logger.getLogger(PSP02_Productor.class.getName()).log(Level.SEVERE, null, ex);
}}}}
Metodo Main:

import java.util.logging.Level;
import java.util.logging.Logger;
public class PSP_tarea02 {
public static void main(String[] args) {
try {PSP02_Bufer b = new PSP02_Bufer(6);
PSP02_Productor p = new PSP02_Productor(b);
PSP02_Consumidor c = new PSP02_Consumidor(b);
p.start();
Thread.sleep(3000);
c.start();
p.join();
c.join();
System.out.println("Programa terminada");
} catch (InterruptedException ex) {
Logger.getLogger(PSP_tarea02.class.getName()).log(Level.SEVERE, null, ex);}}}

El resultado:
Ejers. 2

Primero creamos clase Mesa con array de Semaphore. Creamos métodos para palitos de
izquierda y derecha. También añadimos métodos takechopsticks y putchopsticks; si un filósofo
coge palitos siguiente tiene que esperar.

Mesa

import java.util.concurrent.Semaphore;

class Mesa {
private Semaphore[] chopsticks;

public Mesa(int numPalillos){


this.chopsticks = new Semaphore[numPalillos];
for (int i = 0; i < numPalillos; i++) {
this.chopsticks[i] = new Semaphore(1);}}

public int chopsticksLeft(int i){return i;}


public int chopsticksRight(int i){if(i == 0){
return this.chopsticks.length - 1;
}else{return i - 1;}}
public void takeChopsticks(int persona) throws InterruptedException{
this.chopsticks[this.chopsticksLeft(persona)].acquire();
this.chopsticks[this.chopsticksRight(persona)].acquire();}
public void putChopsticks(int persona){
this.chopsticks[this.chopsticksLeft(persona)].release();
this.chopsticks[this.chopsticksRight(persona)].release();}}

Creamos otro clase Philosofo con metodos run() , esperando(), comiendo().

Philosofo

import java.util.logging.Level;
import java.util.logging.Logger;

class Philosofo extends Thread {


private Mesa mesa;
private int persona;
private int indicePersona;

public Philosofo(Mesa m, int persona){


this.persona = persona;
this.indicePersona = persona - 1;
this.mesa = m;
}
@Override
public void run(){
while(true){
try {this.esperando();
System.out.println("Filosofo " + this.persona + " tiene hambre");
mesa.takeChopsticks(this.indicePersona);
this.comiendo();
System.out.println("Filosofo " + this.persona + " termina comer, tiene palitos
libres: " + (this.mesa.chopsticksLeft(this.indicePersona) + 1) + ", " +
(this.mesa.chopsticksRight(this.indicePersona) + 1));
mesa.putChopsticks(this.indicePersona);
} catch (InterruptedException ex) {
Logger.getLogger(Philosofo.class.getName()).log(Level.SEVERE, null, ex);}}}
public void esperando() throws InterruptedException{
System.out.println("Filosofo " + this.persona + " esperando");
Thread.sleep(2000);}
public void comiendo() throws InterruptedException{
System.out.println("Filosofo " + this.persona + " comiendo");
Thread.sleep(2000);}}

Main

public class PSP_02_Pholosofos {

public static void main(String[] args) {


Mesa m = new Mesa(5);
for (int i = 1; i <= 5; i++) {
Philosofo f = new Philosofo(m, i);
f.start();}}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy