关于多线程中使用while做循环而不使用if的解释
2013-02-06 15:55
309 查看
Java代码
![](http://changaiqing.iteye.com/images/icon_copy.gif)
![](http://changaiqing.iteye.com/images/icon_star.png)
![](http://changaiqing.iteye.com/images/spinner.gif)
package com.thread.example;
import java.util.LinkedList;
public class Example10 {
private LinkedList<Object> myList = new LinkedList<Object>();
private int MAX = 10;
public Example10() {
super();
}
public void start(){
new Producerder().start();
new Consumereder().start();
}
class Producerder extends Thread {
@Override
public void run() {
for (int i = 0; i < 15; i++) {
try {
this.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (myList) {
try {
while(myList.size() == MAX){
System.out.println("warning: it's full!");
myList.wait();
}
//*************把这一段注释掉********************
Object o = new Object();
if(myList.add(o)){
System.out.println("Producer: " + o);
myList.notify();
}
//*******************************
//把上面注释掉后,启用下面一行后。你再在Consumereder中分别是用while和if看下输出结果
//myList.notify();
} catch (Exception e) {
System.out.println("producer is interrupted!");
}
}
}
}
}
class Consumereder extends Thread {
@Override
public void run() {
for (int i = 0; i < 15; i++) {
try {
this.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (myList) {
try {
while(myList.size() == 0){
System.out.println("warning: it's empty!");
myList.wait();
}
System.out.println("oooooooooooooooooo");
Object o = myList.removeLast();
System.out.println("Consumereder" + o);
myList.notify();
} catch (Exception e) {
System.out.println("consumer is interrupted!");
}
}
}
}
}
public static void main(String[] args) {
Example10 ex10 = new Example10();
ex10.start();
}
}
注:根据代码中注释的操作运行一次,你就明白什么意思了;
如果生产者没有生产,就调用唤醒(notify)。消费者的判断会出问题(如果使用if)
这就是为什么使用while而不是if的原因
![](http://changaiqing.iteye.com/images/icon_copy.gif)
![](http://changaiqing.iteye.com/images/icon_star.png)
![](http://changaiqing.iteye.com/images/spinner.gif)
package com.thread.example;
import java.util.LinkedList;
public class Example10 {
private LinkedList<Object> myList = new LinkedList<Object>();
private int MAX = 10;
public Example10() {
super();
}
public void start(){
new Producerder().start();
new Consumereder().start();
}
class Producerder extends Thread {
@Override
public void run() {
for (int i = 0; i < 15; i++) {
try {
this.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (myList) {
try {
while(myList.size() == MAX){
System.out.println("warning: it's full!");
myList.wait();
}
//*************把这一段注释掉********************
Object o = new Object();
if(myList.add(o)){
System.out.println("Producer: " + o);
myList.notify();
}
//*******************************
//把上面注释掉后,启用下面一行后。你再在Consumereder中分别是用while和if看下输出结果
//myList.notify();
} catch (Exception e) {
System.out.println("producer is interrupted!");
}
}
}
}
}
class Consumereder extends Thread {
@Override
public void run() {
for (int i = 0; i < 15; i++) {
try {
this.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (myList) {
try {
while(myList.size() == 0){
System.out.println("warning: it's empty!");
myList.wait();
}
System.out.println("oooooooooooooooooo");
Object o = myList.removeLast();
System.out.println("Consumereder" + o);
myList.notify();
} catch (Exception e) {
System.out.println("consumer is interrupted!");
}
}
}
}
}
public static void main(String[] args) {
Example10 ex10 = new Example10();
ex10.start();
}
}
package com.thread.example; import java.util.LinkedList; public class Example10 { private LinkedList<Object> myList = new LinkedList<Object>(); private int MAX = 10; public Example10() { super(); } public void start(){ new Producerder().start(); new Consumereder().start(); } class Producerder extends Thread { @Override public void run() { for (int i = 0; i < 15; i++) { try { this.sleep(1000); } catch (Exception e) { e.printStackTrace(); } synchronized (myList) { try { while(myList.size() == MAX){ System.out.println("warning: it's full!"); myList.wait(); } //*************把这一段注释掉******************** Object o = new Object(); if(myList.add(o)){ System.out.println("Producer: " + o); myList.notify(); } //******************************* //把上面注释掉后,启用下面一行后。你再在Consumereder中分别是用while和if看下输出结果 //myList.notify(); } catch (Exception e) { System.out.println("producer is interrupted!"); } } } } } class Consumereder extends Thread { @Override public void run() { for (int i = 0; i < 15; i++) { try { this.sleep(100); } catch (Exception e) { e.printStackTrace(); } synchronized (myList) { try { while(myList.size() == 0){ System.out.println("warning: it's empty!"); myList.wait(); } System.out.println("oooooooooooooooooo"); Object o = myList.removeLast(); System.out.println("Consumereder" + o); myList.notify(); } catch (Exception e) { System.out.println("consumer is interrupted!"); } } } } } public static void main(String[] args) { Example10 ex10 = new Example10(); ex10.start(); } }
注:根据代码中注释的操作运行一次,你就明白什么意思了;
如果生产者没有生产,就调用唤醒(notify)。消费者的判断会出问题(如果使用if)
这就是为什么使用while而不是if的原因
相关文章推荐
- Swift学习——使用if和switch来进行条件操作,使用for,while,和do-while来进行循环(三)
- ASP中if语句、select 、while循环的使用方法
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法
- 关于pthread_cond_wait使用while循环判断的理解
- 选择 | 循环 | 语句(if,if……else,switch,while,for)的理解和使用
- python中for、while循环、if嵌套的使用
- Swift学习——使用if和switch来进行条件操作,使用for,while,和do-while来进行循环(三)
- 实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字(for、while、if、else、switch、case)。
- 多线程情况下建议使用while而不是if
- 关于while true 循环中 try catch块的使用
- 关于使用while循环时,括号内用自加自减作条件的一个疑问
- 并发编程学习笔记 三------- 关于在if中和while中使用wait(),唤醒之后的区别
- 关于Smarty中循环的使用(foreach、section)和if的使用
- 关于pthread_cond_wait使用while循环判断的理解
- 多线程操作中为什么使用while而不是if来做判断状态是否就绪
- 循环与递归--不使用if,while,for,do—while语句来输出所有比已知数N小的自然数
- c++中的结构化语句 判断语句if 分支语句switch 循环语句 while 和 do while 循环语句for的使用
- 关于“使用xmlspy编写xsl文件时候,在xsl解释xml文件的时候总是使用utf-16编码”的解决办法
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
- 使用if和goto语句构造循环