public class ProducerCustomer
{
public static void main(String[] args)
{
SycStack st = new SycStack("馒头框");
//st.push(new ManTo(100));
Producer producer = new Producer(st);
Customer customer = new Customer(st);
Thread thread1 = new Thread(producer);
Thread thread2 = new Thread(customer);
thread1.start();
thread2.start();
}
}class ManTo
{
public int id;
public ManTo(int id)
{
this.id = id;
}
public String toString()
{
return "===馒头->" + id;
}
}class SycStack
{
public String name;
int index = 0;
ManTo[] blacket = new ManTo[6];
public SycStack(String str)
{
name = str;
}
public synchronized void push(ManTo mantou)
{
while(index == blacket.length)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
this.notify();
blacket[index] = mantou;
index++;
}
public synchronized ManTo pop()
{
while(index == 0)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
this.notify();
index--;
return blacket[index];
}
}class Producer implements Runnable
{
SycStack st = null;
Producer(SycStack st)
{
this.st = st;
}
public void run()
{
for(int i=0; i<20; i++)
{
ManTo manTo = new ManTo(i);
st.push(manTo);
System.out.println("我生产了" + manTo);
}
}
}class Customer implements Runnable
{
SycStack st = null;
Customer(SycStack st)
{
this.st = st;
}
public void run()
{
for(int i=0; i<20; i++)
{
ManTo manTo = st.pop();
System.out.println("我消费了" + manTo);
}
}
}
运行结果如下:
{
public static void main(String[] args)
{
SycStack st = new SycStack("馒头框");
//st.push(new ManTo(100));
Producer producer = new Producer(st);
Customer customer = new Customer(st);
Thread thread1 = new Thread(producer);
Thread thread2 = new Thread(customer);
thread1.start();
thread2.start();
}
}class ManTo
{
public int id;
public ManTo(int id)
{
this.id = id;
}
public String toString()
{
return "===馒头->" + id;
}
}class SycStack
{
public String name;
int index = 0;
ManTo[] blacket = new ManTo[6];
public SycStack(String str)
{
name = str;
}
public synchronized void push(ManTo mantou)
{
while(index == blacket.length)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
this.notify();
blacket[index] = mantou;
index++;
}
public synchronized ManTo pop()
{
while(index == 0)
{
try
{
this.wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
this.notify();
index--;
return blacket[index];
}
}class Producer implements Runnable
{
SycStack st = null;
Producer(SycStack st)
{
this.st = st;
}
public void run()
{
for(int i=0; i<20; i++)
{
ManTo manTo = new ManTo(i);
st.push(manTo);
System.out.println("我生产了" + manTo);
}
}
}class Customer implements Runnable
{
SycStack st = null;
Customer(SycStack st)
{
this.st = st;
}
public void run()
{
for(int i=0; i<20; i++)
{
ManTo manTo = st.pop();
System.out.println("我消费了" + manTo);
}
}
}
运行结果如下:
解决方案 »
- 重写了equals和hashcode方法为什么用==判断返回false???
- 我的系统是64位的JDK请问高手们64位的JDK怎样才能转换成32位的
- 小弟刚学JAVA,问个问题希望大大们回答
- 有第三方或者引用时简单一点的分页吗?就是只要在需要分页的Jsp上include分页的jsp就可以实现功能的那种比较方便好用的分页模块。
- 如何创建文件夹和文件?
- 用socket发送的数据和接收到的数据不一致是什么问题?
- 请帮忙,调用可执行文件的问题!
- 一个小问题,请大家帮忙
- 求如何实现这个布局?1000分
- java语句耗时问题
- JInternalFrame最小化问题,请高手指教?
- myeclips 连接 mysql 报ClassNotFoundException错误
this.notify();
blacket[index] = mantou;
index++;
需要调整个顺序,修改为:
blacket[index] = mantou;
index++;
this.notify();
对于pop操作同理也要这样做,只有等栈的操作结束了才能唤醒其他线程