Question
Write One program for Cosumer-Producer Problem?

Answers

package com.in.thread;

class ProducerConsumer{

private int count;

public synchronized void consume(){

if(count==0){

try{

wait();

}catch(Exception e){e.printStackTrace();}

}

System.out.println(“Consume() –“+ count);

count–;

try{

Thread.sleep(1000);

}catch(Exception e){

e.printStackTrace();

}

}

public synchronized void produce(){

System.out.println(“produce() –“+ count++);

notifyAll();

}
}

public class ProducerConsumerTest implements Runnable{

ProducerConsumer pc1;

boolean isConsume;

int i=0;

public ProducerConsumerTest(ProducerConsumer pc, boolean c){

this.pc1=pc;

this.isConsume=c;

}

public static void main(String[] args) {

ProducerConsumer pc=new ProducerConsumer();

Thread pro=new Thread(new ProducerConsumerTest(pc,false));

Thread con=new Thread(new ProducerConsumerTest(pc,true));

pro.start();

con.start();

}

public void run() {

for(;i<10;i++){

if(i%2==0){

pc1.produce();

}else

{

pc1.consume();

}

}

}

}   Your Comment




More Software,producer Questions..
What is the inputsplit in map reduce software?

What is software configuration management?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?

What are the minimum system requirements to run Photoshop? Is it possible to run Photoshop over linux?

Which oracle package is used to manage the oracle lock management services?

What is Latch Up? Explain Latch Up with cross section of a CMOS Inverter. How do you avoid Latch Up?

What is marker interface?

What types of partitioning are there for BW?



Search
Can you Answer!!
  • Q The top most bar in any application window is the ____ which displays the name of the document or application :
  • Q What are the different types of stocks that exist?
  • Q 1st Heli Expo India and International Civil Helicopter Conclave-2017 began in
  • Q Which holder is recommended for the 1/4" array and 1/4" working standard for pcb® 378 series of microphones?
  • Q TCP and IP protocols operate at which ISO/OSI layers?
  • Q Who has been selected for the Tata Literature Live! Lifetime Achievement Award 2017?
  • Q What is multiThreading in .NET?
  • Q List the roles which are played by international politics in civil wars?
  • Q What is the difference between one side and both sides in extrusion?
  • Q Who was the first chief minister of Uttaranchal
  • Q What quality of voice is mostly used in speaking and reading?