Results 1 to 5 of 5
- 12-10-2012, 07:05 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
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();
}
}
- 12-10-2012, 09:32 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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.
- 12-10-2012, 10:45 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Re: Producer Consumer Synchronization Problem
thank doWhile.this is my first post ...how to put the code in the code tags
- 12-11-2012, 07:35 AM #4
Re: Producer Consumer Synchronization Problem
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-12-2012, 09:21 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Consumer/Producer with random numbers
By devo22 in forum Threads and SynchronizationReplies: 1Last Post: 11-13-2011, 06:57 PM -
producer/consumer problem
By concaf in forum Threads and SynchronizationReplies: 1Last Post: 09-20-2011, 02:07 PM -
Producer-Consumer Problem
By kendel in forum Threads and SynchronizationReplies: 1Last Post: 03-04-2011, 01:09 PM -
Producer Consumer Synchronization Problem
By rushhour in forum Threads and SynchronizationReplies: 3Last Post: 11-23-2010, 07:44 PM -
Implementation of the Producer/Consumer problem in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks