package code;class CubbyHole
{
public int content;
private boolean available=false; public synchronized int get()
{
while(available==true)
{
try{
wait();
}catch(InterruptedException e)
{
System.out.println(e);
}
}
available=false;
notifyAll();
System.out.println("Consumer:"+" get:"+ content);
return content;
} public synchronized void put(int value){
while(available==false)
{
try{
wait();
}catch(InterruptedException e)
{
System.out.println(e);
}
content=value;
available=true;
notifyAll();
System.out.println("Consumer:"+" put:"+ value);
}
}
}
class Producer extends Thread
{
private CubbyHole cubbyhole;
public Producer(CubbyHole c)
{
cubbyhole=c;
}
public void run()
{
for(int i=0;i<10;i++)
{
cubbyhole.put(i);
// System.out.println("Producer"+" put:"+ i);
try{
sleep(1000);
}catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
class Consumer extends Thread
{
private CubbyHole cubbyhole;
public Consumer(CubbyHole c)
{
cubbyhole=c;
}
public void run()
{
int value;
for(int i=0;i<10;i++)
{
value=cubbyhole.get();
try{
sleep(1000);
}catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
public class Demo
{
public static void main(String[] args)
{
CubbyHole h=new CubbyHole();
Producer p=new Producer(h);
Consumer c=new Consumer(h);
p.start();
c.start();
}
}
不知道这程序错在哪,就是不能达到我要的效果,就是产生一个,取一个!
{
public int content;
private boolean available=false; public synchronized int get()
{
while(available==true)
{
try{
wait();
}catch(InterruptedException e)
{
System.out.println(e);
}
}
available=false;
notifyAll();
System.out.println("Consumer:"+" get:"+ content);
return content;
} public synchronized void put(int value){
while(available==false)
{
try{
wait();
}catch(InterruptedException e)
{
System.out.println(e);
}
content=value;
available=true;
notifyAll();
System.out.println("Consumer:"+" put:"+ value);
}
}
}
class Producer extends Thread
{
private CubbyHole cubbyhole;
public Producer(CubbyHole c)
{
cubbyhole=c;
}
public void run()
{
for(int i=0;i<10;i++)
{
cubbyhole.put(i);
// System.out.println("Producer"+" put:"+ i);
try{
sleep(1000);
}catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
class Consumer extends Thread
{
private CubbyHole cubbyhole;
public Consumer(CubbyHole c)
{
cubbyhole=c;
}
public void run()
{
int value;
for(int i=0;i<10;i++)
{
value=cubbyhole.get();
try{
sleep(1000);
}catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
public class Demo
{
public static void main(String[] args)
{
CubbyHole h=new CubbyHole();
Producer p=new Producer(h);
Consumer c=new Consumer(h);
p.start();
c.start();
}
}
不知道这程序错在哪,就是不能达到我要的效果,就是产生一个,取一个!
解决方案 »
- 急问:不知道出了什么问题,有关java空指针问题?
- 将file1.txt复制到file2.txt中,源文件名和目标文件名作为命令行参数输入?
- 求电子版的<<网络机器人java编程指南>>jeff heaton著
- 急询:如何在scrolPane中显示一个超链接,点击后用IE打开一个网页??
- java多线程问题(wait(time))
- Cannot find ActionMappings or ActionFormBeans collection??
- 大家来评价一下几种java开发工具
- 新手的问题。。
- 各位兄弟,急救我是初学者,关于JBULID的问题?
- JSP中内置对象
- SOCKET问题
- java学习软件
if (!available) {
try {
wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
available = false;
notifyAll();
System.out.println("Consumer:" + " get:" + content);
//
// while (available == true) {
// try {
// wait();
// } catch (InterruptedException e) {
// System.out.println(e);
// }
// available = false;
// notifyAll();
// System.out.println("Consumer:" + " get:" + content);
// }
return content;
} public synchronized void put(int value) {
if (available) {
try {
wait();
} catch (InterruptedException e) {
System.out.println(e);
}
}
content = value;
available = true;
notifyAll();
System.out.println("Consumer:" + " put:" + value);
// while (available == false) {
// try {
// wait();
// } catch (InterruptedException e) {
// System.out.println(e);
// }
// content = value;
// available = true;
// notifyAll();
// System.out.println("Consumer:" + " put:" + value);
// }
}