JAVA生产者消费者问题

yuanjize1996 2016-04-21 05:30:01
我的代码:
import org.junit.Test;

public class Main2 {
int MAX_num = 1000;
static int apple = 200;

@Test
public void main() {
Runnable creator = new Runnable() {
public void run() {
create();
}
private void create() {
while (true) {
if (apple < MAX_num) {
synchronized (Main2.this) {
apple++;
System.out.println("apple已有:" + apple);
if (apple == MAX_num) {
try {
Main2.this.notifyAll();
Main2.this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
};
Runnable customeRunnable = new Runnable() {
@Override
public void run() {
custom();
}
private void custom() {
while (true) {
if (apple != 0) {
synchronized (Main2.this) {
apple--;
System.out.println("apple剩下:" + apple);
if (apple == 0) {
Main2.this.notifyAll();
try {
Main2.this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
};
Thread customerThread = new Thread(customeRunnable);
Thread creatorThread = new Thread(creator);
creatorThread.start();
customerThread.start();
}
}



最后的运行结果:
apple已有:201
apple已有:202
apple已有:203
apple剩下:202
apple剩下:201
apple剩下:200
apple剩下:199
apple剩下:198
apple剩下:197
apple剩下:196
apple剩下:195
apple剩下:194
apple剩下:193
apple剩下:192
apple剩下:191
apple剩下:190
apple剩下:189
apple剩下:188
apple剩下:187
apple剩下:186
apple剩下:185
apple剩下:184
apple已有:185
apple已有:186
apple已有:187
apple已有:188
apple已有:189
apple已有:190
apple已有:191
apple已有:192
apple已有:193
apple已有:194
apple已有:195
apple已有:196
apple已有:197
apple已有:198
apple已有:199
apple已有:200
apple已有:201
apple已有:202
apple已有:203
apple已有:204
apple已有:205
apple已有:206
apple剩下:205
apple剩下:204
apple剩下:203
apple剩下:202
apple剩下:201
apple剩下:200
apple剩下:199
apple剩下:198
apple剩下:197
apple剩下:196
apple剩下:195
apple剩下:194
apple剩下:193
apple剩下:192
apple剩下:191
apple剩下:190
apple剩下:189
apple剩下:188
apple剩下:187
apple剩下:186
apple剩下:185
apple剩下:184
apple剩下:183
apple剩下:182
apple剩下:181
apple剩下:180
apple剩下:179
apple剩下:178
apple剩下:177
apple剩下:176
apple剩下:175
apple剩下:174
apple剩下:173
apple剩下:172
apple剩下:171
apple剩下:170
apple剩下:169
apple剩下:168
apple剩下:167
apple剩下:166
apple剩下:165
apple剩下:164
apple剩下:163
apple剩下:162
apple剩下:161
apple剩下:160
apple剩下:159
apple剩下:158
apple剩下:157
apple剩下:156
apple剩下:155
apple剩下:154
apple剩下:153
apple剩下:152
apple剩下:151
apple剩下:150
apple剩下:149
apple剩下:148
apple剩下:147
apple剩下:146
apple剩下:145
apple剩下:144
apple剩下:143
apple剩下:142
apple剩下:141
apple剩下:140
apple剩下:139
apple剩下:138
apple剩下:137
apple剩下:136
apple剩下:135
apple剩下:134
apple剩下:133
apple剩下:132
apple剩下:131
apple剩下:130
apple剩下:129
apple剩下:128
apple剩下:127
apple剩下:126
apple剩下:125
apple剩下:124
apple剩下:123
apple剩下:122
apple剩下:121
apple剩下:120
apple剩下:119
apple剩下:118
apple剩下:117
apple剩下:116
apple剩下:115
apple剩下:114
apple剩下:113
apple剩下:112
apple剩下:111
apple剩下:110
apple剩下:109
apple剩下:108


到这里程序就停止了,我写的死循环不应该停止啊,而且要是是思索的话程序应该是等待,不应该是停止啊,求教各位这是怎么回事。

...全文
287 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanjize1996 2016-04-28
  • 打赏
  • 举报
回复
多谢多谢
引用 2 楼 zhuangqingch 的回复:
楼主是用junit运行代码吗?如果是的话,这个跟junit的代码执行机制有关,具体可以看下这个帖子: 用Junit测试简单的线程出现了问题
引用 2 楼 zhuangqingch 的回复:
楼主是用junit运行代码吗?如果是的话,这个跟junit的代码执行机制有关,具体可以看下这个帖子: 用Junit测试简单的线程出现了问题
zhuangqingch 2016-04-25
  • 打赏
  • 举报
回复
楼主是用junit运行代码吗?如果是的话,这个跟junit的代码执行机制有关,具体可以看下这个帖子: 用Junit测试简单的线程出现了问题
乔不思 2016-04-21
  • 打赏
  • 举报
回复
我运行你的代码 就没问题啊
public class Test {
	 static int MAX_num = 1000;
	 static int apple = 200;
	public static void main(String[] args) {
		 Runnable creator = new Runnable() {
	            public void run() {
	                create();
	            }
	            private void create() {
	                while (true) {
	                    if (apple < MAX_num) {
	                        synchronized (Test.class) {
	                            apple++;
	                            System.out.println("apple已有:" + apple);
	                            if (apple == MAX_num) {
	                                try {
	                                    Test.class.notifyAll();
	                                    Test.class.wait();
	                                } catch (InterruptedException e) {
	                                    e.printStackTrace();
	                                }
	                            }
	                        }
	                    }
	                }
	            }
	        };
	        
	        
	        Runnable customeRunnable = new Runnable() {
	            @Override
	            public void run() {
	                custom();
	            }
	            private void custom() {
	                while (true) {
	                    if (apple != 0) {
	                        synchronized (Test.class) {
	                            apple--;
	                            System.out.println("apple剩下:" + apple);
	                            if (apple == 0) {
	                            	Test.class.notifyAll();
	                                try {
	                                	Test.class.wait();
	                                } catch (InterruptedException e) {
	                                    e.printStackTrace();
	                                }
	                            }
	                        }
	                    }
	                }
	            }
	        };
	        Thread customerThread = new Thread(customeRunnable);
	        Thread creatorThread = new Thread(creator);
	        creatorThread.start();
	        customerThread.start();
	}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧