Java
Home

Threads

 

/*
* Sincronizador.java
*
* Created on 18 de Março de 2007, 22:21
*/

package threads;

/**
*
* @author adrw
*/

public class Sincronizador
{
   private int valor_total = -1;
   private boolean escrever = true;


   // método sincronizado permite que somente uma thread de cada vez
   // invoque este método para atribuir o valor
   // para um objeto Sincronizador particular

   public synchronized void setSincronizado( int valor )
   {
      while ( escrever == false )
      {
         // thread q chamou este metodo precisa esperar
         try
         {
            wait();
         }
         catch ( InterruptedException exception )
         {
            exception.printStackTrace();
         }

      }

      System.err.println( Thread.currentThread().getName() + " setando valor para " + valor );

      valor_total = valor;
      escrever = false;

      // diz a thread em espera para se tornar pronta
      notify();
   }


   public synchronized int getSincronizado( )
   {
      while ( escrever == true )
      {
         // thread q chamou este metodo precisa esperar
         try
         {
            wait();
         }
         catch ( InterruptedException exception )
         {
            exception.printStackTrace();
         }

      }

      System.err.println( Thread.currentThread().getName() + " lendo valor: " + valor_total );

      escrever = true;

      // diz a thread em espera para se tornar pronta
      notify();

      return valor_total;
   }

   /** Creates a new instance of Sincronizador */
   public Sincronizador() {
   }

}


/*
* Produzir.java
*
* Created on 18 de Março de 2007, 21:55
*/

package threads;


/**
*
* @author adrw
*/

public class Produzir extends Thread
{
   private Sincronizador sinc;

   /** Creates a new instance of Produzir */
   public Produzir( Sincronizador vsinc )
   {
      super("Produzir");
      sinc = vsinc;
   }

   public void run()
   {
      for( int c=1; c<=10; c++ )
      {
         try {
            Thread.sleep( (int)( Math.random() * 3000 ));
         }
         catch( InterruptedException exception )
         {
            System.err.println( exception.toString() );
         }

         sinc.setSincronizado( c );
      }


   }
   
   //System.err.println( " Finalizado produção do valor " );

}


/*
* Consumir.java
*
* Created on 18 de Março de 2007, 22:13
*/

package threads;


/**
*
* @author adrw
*/

public class Consumir extends Thread
{

   private Sincronizador sinc;

   /** Creates a new instance of Consumir */
   public Consumir( Sincronizador vsinc )
   {
      super("Consumidor");
      sinc = vsinc;

   }


   public void run()
   {
      int valor = 0, sum = 0;

      while ( valor != 10 )
      {
         try
         {
            Thread.sleep( (int)(Math.random() * 3000 ) );
         }
         catch ( InterruptedException exception )
         {
            System.err.println( exception.toString() );
         }
   
         valor = sinc.getSincronizado();
         sum += valor;
      }
      
      System.err.println( getName() + " retirado valor total " + sum + "Terminado " + getName() );
   }
}


/*
* Main.java
*
* Created on 18 de Março de 2007, 21:52
*/

package threads;

/**
*
* @author adrw
*/

public class Main
{

   /** Creates a new instance of Main */
   public Main() {
   }

   /**
   * @param args the command line arguments
   */

   public static void main(String[] args) {

      Sincronizador sinc = new Sincronizador();

      Produzir producer = new Produzir( sinc );
      Consumir consumer = new Consumir( sinc );


      producer.start();
      consumer.start();

   }
}