public class ProducerConsumer{
public static void main(String[] args){
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Producer pp = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(pp).start();
new Thread(p).start();
new Thread(c).start();
}
}class WoTou{
int id;
WoTou(int id){
this.id=id;
}
public String toString(){
return "WoTou:"+id;
}
}class SyncStack{
int index = 0;
WoTou[] arrwt=new WoTou[6];
public synchronized void push(WoTou wt){
while(index==arrwt.length){
try{
this.wait();
}catch(InterruptedException e){
System.out.println(e);
}
}
this.notifyAll();
arrwt[index]=wt;
index++;
} public synchronized WoTou pop(){
while(index==0){
try{
this.wait();
}catch(InterruptedException e){
System.out.println(e);
}
}
this.notifyAll();
index--;
return arrwt[index];
}
}class Producer implements Runnable{
SyncStack ss=null;
Producer(SyncStack ss){
this.ss=ss;
}
public void run(){
for(int i=0;i<10;i++){
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产:"+wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class Consumer implements Runnable{
SyncStack ss = null;
Consumer(SyncStack ss){
this.ss=ss;
}
public void run(){
for(int i=0;i<10;i++){
WoTou wt=ss.pop();
System.out.println("消费:"+wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
当SyncStack类里的WoTou[] arrwt=new WoTou[6] 元素小于10就会死锁,好想是一直被 wait 着。
为什么notifyAll不会唤醒pop()方法。
是哪里的原因,始终找不到。这是尚学堂视频里的例子。比对后也找不到,不明白。
请大家帮忙找找原因。
public static void main(String[] args){
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Producer pp = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(pp).start();
new Thread(p).start();
new Thread(c).start();
}
}class WoTou{
int id;
WoTou(int id){
this.id=id;
}
public String toString(){
return "WoTou:"+id;
}
}class SyncStack{
int index = 0;
WoTou[] arrwt=new WoTou[6];
public synchronized void push(WoTou wt){
while(index==arrwt.length){
try{
this.wait();
}catch(InterruptedException e){
System.out.println(e);
}
}
this.notifyAll();
arrwt[index]=wt;
index++;
} public synchronized WoTou pop(){
while(index==0){
try{
this.wait();
}catch(InterruptedException e){
System.out.println(e);
}
}
this.notifyAll();
index--;
return arrwt[index];
}
}class Producer implements Runnable{
SyncStack ss=null;
Producer(SyncStack ss){
this.ss=ss;
}
public void run(){
for(int i=0;i<10;i++){
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产:"+wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class Consumer implements Runnable{
SyncStack ss = null;
Consumer(SyncStack ss){
this.ss=ss;
}
public void run(){
for(int i=0;i<10;i++){
WoTou wt=ss.pop();
System.out.println("消费:"+wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
当SyncStack类里的WoTou[] arrwt=new WoTou[6] 元素小于10就会死锁,好想是一直被 wait 着。
为什么notifyAll不会唤醒pop()方法。
是哪里的原因,始终找不到。这是尚学堂视频里的例子。比对后也找不到,不明白。
请大家帮忙找找原因。
解决方案 »
- 如何定义好一个较为通用并扩展性较好的这样的一个类?
- 初涉JNI编程与麻烦,帮忙看看
- 关于static类的问题
- 偶滴个神啊,SMTP报文居然将近6000个字节
- 赴日考核!!!!!!!!!!!!!!!
- 请问静态变量声明时未被赋值会被分配空间吗?
- 已经对读取的文经过处理,现在想把它存入另一个一个文,应该用什么命令,请各位大侠帮忙!
- 在启动servlet的同时是否可以在开启一个线程
- ArrayList已经取代了Vector为什么Sun的例子里还在用Vector,包括最新的例子,这是为什么?
- 如何在BorderLayout中EAST,WEST等的宽度可以改吗?如何改?高手指点
- 有关于除0的问题
- 如何用JAVAScript网页上实现,两次密码输入是否一致??
我再加一个消费者线程,或者 消费者的for变成20 让取的次数与生产的次数相当就可以了。