<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>consumer problem | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/consumer-problem/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sun, 04 Oct 2009 15:23:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>Java program for implentation of consumer problem using inter thread communication</title>
		<link>https://studentprojects.in/software-development/java/java-programs/advanced/java-program-for-implentation-of-consumer-problem-using-inter-thread-communication/</link>
					<comments>https://studentprojects.in/software-development/java/java-programs/advanced/java-program-for-implentation-of-consumer-problem-using-inter-thread-communication/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Sun, 04 Oct 2009 15:23:29 +0000</pubDate>
				<category><![CDATA[Advanced programs]]></category>
		<category><![CDATA[Java program]]></category>
		<category><![CDATA[consumer problem]]></category>
		<category><![CDATA[inter thread communication]]></category>
		<category><![CDATA[inter threads in java]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=895</guid>

					<description><![CDATA[<p>Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication</p>
<p>The post <a href="https://studentprojects.in/software-development/java/java-programs/advanced/java-program-for-implentation-of-consumer-problem-using-inter-thread-communication/">Java program for implentation of consumer problem using inter thread communication</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication.</p>
<pre lang="java" escaped="true" line="1">
class Q
{
	int n;
	boolean valueSet=false;
	synchronized int get()
	{
		if(!valueSet)
		try
		{
			wait();
		}
		catch(InterruptedException e)
		{
			System.out.println("Interrupted Exception caught");
		}
		System.out.println("Got:"+n);
		valueSet=false;
		notify();
		return n;
	}
	synchronized void put(int n)
	{
		if(valueSet)
		try
		{
			wait();
		}
		catch(InterruptedException e)
		{
			System.out.println("Interrupted Exception caught");
		}
		this.n=n;
		valueSet=true;
		System.out.println("Put:"+n);
		notify();
	}
}
class Producer implements Runnable
{
	Q q;
	Producer(Q q)
	{
		this.q=q;
		new Thread(this,"Producer").start();
	}
	public void run()
	{
		int i=0;
		while(true)
		{
			q.put(i++);
		}
	}
}
class Consumer implements Runnable
{
	Q q;
	Consumer(Q q)
	{
		this.q=q;
		new Thread(this,"Consumer").start();
	}
	public void run()
	{
		while(true)
		{
			q.get();
		}
	}
}
class ProdCons
{
	public static void main(String[] args)
	{
		Q q=new Q();
		new Producer(q);
		new Consumer(q);
		System.out.println("Press Control-c to stop");
	}
}
</pre>
<p><strong>Output:</strong></p>
<p>Put:1<br />
Got:1<br />
Put:2<br />
Got:2<br />
Put:3<br />
Got:3<br />
Put:4<br />
Got:4<br />
Put:5<br />
Got:5<br />
Put:6<br />
Got:6<br />
Put:7<br />
Got:7<br />
Put:8<br />
Got:8<br />
Put:9<br />
Got:9<br />
Put:10<br />
Got:10<br />
Put:11<br />
Got:11<br />
Put:12<br />
Got:12<br />
Put:13<br />
Got:13<br />
Put:14<br />
Got:14</p><p>The post <a href="https://studentprojects.in/software-development/java/java-programs/advanced/java-program-for-implentation-of-consumer-problem-using-inter-thread-communication/">Java program for implentation of consumer problem using inter thread communication</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/java/java-programs/advanced/java-program-for-implentation-of-consumer-problem-using-inter-thread-communication/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
