Results 1 to 5 of 5
  1. #1
    djery7 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Producer Consumer Synchronization Problem

    I want to implement in class - producer (Multithreaded) a method of generating multiple child processes competing simultaneously vying for the queue buffer. Example: instead the producer produce an entity at a time, he produced 'scale random over ONE entity at a time, at least two to two entities at a time. The buffer must have a size limited to entities receiving. Using a finite array.

    already have functional classes. Can someone help me.

    package javaapplication1;
    ObjetoBuffer.java
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author pc9
    */
    public class ObjetoBuffer {
    private int memoria = -1;
    private boolean acessivel = true; // variavel booleana - condicao de escrita






    // metodo de escrita de dados na memoria

    public synchronized void escreveBuffer(int valor)
    {
    while(!acessivel) { // Ai Não permite EScrita
    try {
    wait(); //ESpera-suspende a thread que chamou este metodo
    }
    catch ( InterruptedException e) {
    e.printStackTrace();
    }
    }

    System.err.println( Thread.currentThread().getName() +
    " prodizindo o valor: " + valor );
    this.memoria = valor;

    acessivel = false; // Desativa a memoria para escrita
    notify( ); // liberta uma thread que esta a ESperar devido a um wait( )
    }






    // Metodo de leitura de dados na memoria
    public synchronized int lerBuffer()
    {

    while(acessivel) { // Nao permite Leitura
    try {
    wait( ); //suspende a thread que chamou este metodo
    }
    catch ( InterruptedException e) {
    e.printStackTrace();
    }
    }

    System.err.println( Thread.currentThread().getName() +
    " consumindo o valor: " + this.memoria );

    acessivel = true; // liberta buffer para leitura
    notify(); // liberta uma thread que estah ESPERANDO devido a um wait( )
    return this.memoria;
    }


    }
    -----------------------------------
    Produtor.java

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package javaapplication1;

    /**
    *
    * @author pc9
    */
    // classe Produtor.java
    // Definicao da classe Produtor

    public class Produtor extends Thread {
    private ObjetoBuffer o_Buffer;

    // construtor da Thread Produtor
    public Produtor(ObjetoBuffer dado)
    {
    super( "Produtor" );
    o_Buffer = dado;
    }

    // Thread do Produtor escreverah 10 vezes no buffer em intervalos de tempo aleatorios

    public void run()
    {
    for ( int i = 1; i <= 10; i++ ) {

    // dorme por um tempo aleatorio
    try {
    Thread.sleep( ( int ) ( Math.random() * 3000 ) );
    }

    // tratamento de excecao
    catch( InterruptedException exception ) {
    System.err.println( exception.toString() );
    }

    // chama metodo do objeto buffer

    o_Buffer.escreveBuffer(i);
    }

    System.err.println(getName() + " terminou de produzir");
    }

    }
    // FIM DA CLASSE Produtor

    //***********************
    -----------------------------------------
    Consumidor.java

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package javaapplication1;

    /**
    *
    * @author pc9
    */
    public class Consumidor extends Thread {
    private ObjetoBuffer um_Buffer;

    // Construtor do Consumidor thread object
    public Consumidor(ObjetoBuffer dado)
    {
    super("Consumidor");
    um_Buffer = dado;
    }

    // Thread Consumidor lerah o buffer 10 vezes em intervalos aleatorios

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

    do {

    // dorme por um intervalo aleatorio
    try {
    Thread.sleep( (int) ( Math.random() * 3000 ) );
    }

    // Tratamento de excecao
    catch( InterruptedException exception ) {
    System.err.println( exception.toString() );
    }

    valor = um_Buffer.lerBuffer();
    soma += valor;

    } while ( valor != 10 );

    System.err.println(
    getName() + " terminou de consumir. Totalizou: " + soma);
    }

    }

    // fim da classe Consumidor
    -------------------------------------
    Principal.java
    package javaapplication1;

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */// Classe Principal.java
    // mostra duas threads modificando um objeto compartilhado.


    /**
    *
    * @author pc9
    */
    public class Principal {
    // execução de aplicação
    public static void main( String args[] )
    {
    ObjetoBuffer umBuffer = new ObjetoBuffer();

    // Criacao das threads
    Produtor umProdutor = new Produtor( umBuffer );
    Consumidor umConsumidor = new Consumidor( umBuffer);

    // start threads
    umProdutor.start();
    umConsumidor.start();
    }

    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Producer Consumer Synchronization Problem

    Please don't hijack someone else's post, especially one that is over 2 years old. I have moved your post to its own thread. I would also recommend wrapping your code in the code tags so that it is readable to a human.

  3. #3
    djery7 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Producer Consumer Synchronization Problem

    thank doWhile.this is my first post ...how to put the code in the code tags

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Producer Consumer Synchronization Problem

    Quote Originally Posted by djery7 View Post
    how to put the code in the code tags
    Don't be so helpless. Did you even try to find any resources on this site that would help you answer that yourself?

    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    djery7 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Producer Consumer Synchronization Problem

    Samebody cam help me please...

Similar Threads

  1. Consumer/Producer with random numbers
    By devo22 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-13-2011, 06:57 PM
  2. producer/consumer problem
    By concaf in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-20-2011, 02:07 PM
  3. Producer-Consumer Problem
    By kendel in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-04-2011, 01:09 PM
  4. Producer Consumer Synchronization Problem
    By rushhour in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-23-2010, 07:44 PM
  5. Replies: 0
    Last Post: 04-09-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •