Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

Desenvolva um programa formado por três processos concorrentes, chamados observador, tratador1 e tratador2, que executam uma repetição infinita e sincronizam suas ações com o uso de semáforos. O processo observador lê valores inteiros, que representam a temperatura de um certo dispositivo (Math.random() % 100). Se o valor lido é menor ou igual a 50, deve ser notificado o processo tratador1. Caso o valor seja maior que 50, deve ser notificado o processo tratador2.

 

Diagrama de Classes na Linguagem de Programação Java EmptyBufferException.java FullBufferException.java Buffer.java Observer.java Treater.java Application.java
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação Java

Arquivo EmptyBufferException.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial06.exercicio05;

/**
 * Excecao lancada pela classe Buffer para indicar que o buffer esta vazio
 */
public class EmptyBufferException extends RuntimeException
{
  /**
   * Identificador de serializacao da classe
   */
  private static final long serialVersionUID = 1L;

  /**
   * Construtor padrao
   */
  public EmptyBufferException()
  {
    super();
  }

  /**
   * Construtor para inicializar a mensagem de erro
   *
   * @param message mensagem de erro
   */
  public EmptyBufferException(String message)
  {
    super(message);
  }

  /**
   * Construtor para inicializar a mensagem de erro e a causa do erro
   *
   * @param message mensagem de erro
   * @param cause causa do erro
   */
  public EmptyBufferException(String message, Throwable cause)
  {
    super(message, cause);
  }

  /**
   * Construtor para inicializar a mensagem de erro, a causa do erro,
   * a supressao e o rastreamento da pilha
   *
   * @param message mensagem de erro
   * @param cause causa do erro
   * @param enableSuppression se a supressao esta ativada ou desativada
   * @param writableStackTrace se o rastreamento da pilha deve ser gravavel
   */
  public EmptyBufferException(String message, Throwable cause,
    boolean enableSuppression, boolean writableStackTrace)
  {
    super(message, cause, enableSuppression, writableStackTrace);
  }

  /**
   * Construtor para inicializar a causa do erro
   *
   * @param cause causa do erro
   */
  public EmptyBufferException(Throwable cause)
  {
    super(cause);
  }
}

Arquivo FullBufferException.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial06.exercicio05;

/**
 * Excecao lancada pela classe Buffer para indicar que o buffer esta cheio
 */
public class FullBufferException extends RuntimeException
{
  /**
   * Identificador de serializacao da classe
   */
  private static final long serialVersionUID = 1L;

  /**
   * Construtor padrao
   */
  public FullBufferException()
  {
    super();
  }

  /**
   * Construtor para inicializar a mensagem de erro
   *
   * @param message mensagem de erro
   */
  public FullBufferException(String message)
  {
    super(message);
  }

  /**
   * Construtor para inicializar a mensagem de erro e a causa do erro
   *
   * @param message mensagem de erro
   * @param cause causa do erro
   */
  public FullBufferException(String message, Throwable cause)
  {
    super(message, cause);
  }

  /**
   * Construtor para inicializar a mensagem de erro, a causa do erro,
   * a supressao e o rastreamento da pilha
   *
   * @param message mensagem de erro
   * @param cause causa do erro
   * @param enableSuppression se a supressao esta ativada ou desativada
   * @param writableStackTrace se o rastreamento da pilha deve ser gravavel
   */
  public FullBufferException(String message, Throwable cause,
    boolean enableSuppression, boolean writableStackTrace)
  {
    super(message, cause, enableSuppression, writableStackTrace);
  }

  /**
   * Construtor para inicializar a causa do erro
   *
   * @param cause causa do erro
   */
  public FullBufferException(Throwable cause)
  {
    super(cause);
  }
}

Arquivo Buffer.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial06.exercicio05;

import java.io.Serializable;

/**
 * Buffer de inteiros compartilhado pelo observador e tratador
 */
public class Buffer implements Serializable
{
  /**
   * Identificador de serializacao da classe
   */
  private static final long serialVersionUID = 1L;

  /**
   * Nome do buffer
   */
  private String name;

  /**
   * Valor inteiro
   */
  private Integer value;

  /**
   * Construtor para inicializar o buffer
   */
  public Buffer(String name)
  {
    this.name = name;

    value = null;
  }

  /**
   * Retornar o nome do buffer
   *
   * @return nome do buffer
   */
  public String getName()
  {
    return name;
  }

  /**
   * Verificar se o buffer esta vazio
   *
   * @return true se o buffer esta vazio, false em caso contrario
   */
  public boolean isEmpty()
  {
    return value == null;
  }

  /**
   * Verificar se o buffer esta cheio
   *
   * @return true se o buffer esta cheio, false em caso contrario
   */
  public boolean isFull()
  {
    return value != null;
  }

  /**
   * Inserir um novo valor no buffer
   *
   * @param value novo valor
   * @throws FullBufferException buffer cheio
   */
  public void add(Integer value) throws FullBufferException
  {
    if(!isFull())
    {
      this.value = value;
    }
    else
    {
      throw new FullBufferException();
    }
  }

  /**
   * Remover o valor do buffer
   *
   * @return valor do buffer
   * @throws EmptyBufferException buffer vazio
   */
  public Integer remove() throws EmptyBufferException
  {
    if(!isEmpty())
    {
      Integer aux = value;

      value = null;

      return aux;
    }

    throw new EmptyBufferException();
  }
}

Arquivo Observer.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial06.exercicio05;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Observador do dispositivo
 */
public class Observer implements Runnable
{
  /**
   * Lista de buffers
   */
  private List<Buffer> buffers;

  /**
   * Nome do observador
   */
  private String name;

  /**
   * Construtor para inicializar o observador
   *
   * @param name nome do observador
   * @param buffers lista de buffers
   */
  public Observer(String name, List<Buffer> buffers)
  {
    this.name = name;

    this.buffers = buffers;
  }

  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  @Override
  public void run()
  {
    ThreadLocalRandom random = ThreadLocalRandom.current();

    while(true)
    {
      try
      {
        Integer temperature = random.nextInt(100);
        
        Buffer buffer = null;
        
        if(temperature <= 50)
        {
          buffer = buffers.get(0);
        }
        else
        {
          buffer = buffers.get(1);
        }
        
        synchronized(buffer)
        {
          while(buffer.isFull())
          {
            System.out.print(name + ": ");
            System.out.println(buffer.getName() + " is full!");

            buffer.wait();
          }

          buffer.add(temperature);

          System.out.print(name + ": readed " + temperature);
          System.out.println(" to the " + buffer.getName());

          buffer.notifyAll();
        }
      }
      catch(InterruptedException exception)
      {
        exception.printStackTrace();
      }
    }
  }
}

Arquivo Treater.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial06.exercicio05;

/**
 * Tratador do dispositivo
 */
public class Treater implements Runnable
{
  /**
   * Buffer com a temperatura
   */
  private Buffer buffer;

  /**
   * Nome do tratador
   */
  private String name;

  /**
   * Construtor para inicializar o tratador
   *
   * @param name nome do tratador
   * @param buffer buffer com a temperatura
   */
  public Treater(String name, Buffer buffer)
  {
    this.name = name;

    this.buffer = buffer;
  }

  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  @Override
  public void run()
  {
    while(true)
    {
      try
      {
        synchronized(buffer)
        {
          while(buffer.isEmpty())
          {
            System.out.print(name + ": ");
            System.out.println(buffer.getName() + " is empty!");

            buffer.wait();
          }

          Integer value = buffer.remove();

          System.out.print(name + ": readed " + value);
          System.out.println(" from the " + buffer.getName());

          buffer.notifyAll();
        }
      }
      catch(InterruptedException exception)
      {
        exception.printStackTrace();
      }
    }
  }
}

Arquivo Application.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial06.exercicio05;

import java.util.LinkedList;
import java.util.List;

/**
 * Classe responsavel pela execucao da solucao
 */
public class Application
{
  /**
   * Construtor para inicializar a execucao da solucao
   */
  private Application()
  {

  }

  /**
   * Metodo principal da linguagem de programacao Java
   *
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    List<Buffer> buffers = new LinkedList<>();

    buffers.add(new Buffer("buffer 01"));
    buffers.add(new Buffer("buffer 02"));

    new Thread(new Observer("Observer 01", buffers)).start();

    new Thread(new Treater("Treater 01", buffers.get(0))).start();

    new Thread(new Treater("Treater 02", buffers.get(1))).start();
  }
}