java关于布尔的一个疑问
publicclassResource{privateStringname;privateintcount=1;privatebooleanflag;publicsync...
public class Resource {
private String name;
private int count = 1;
private boolean flag ;
public synchronized void set(String name){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name + count;
count++;
System.out.println(Thread.currentThread().getName()
+ "The producer name is :++++++++++++++ " + this.name);
flag = true;
this.notify();
}
public synchronized void out(){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() +
"The consumer name is : ---------------------------" + this.name);
flag = false;
this.notify();
}
}
这是别人举的生产消息的一个例子,我的疑问是这里直接用if(flag)进行判断,flag的初始布尔值是从哪里弄的怎么进入到if里面去? 展开
private String name;
private int count = 1;
private boolean flag ;
public synchronized void set(String name){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name + count;
count++;
System.out.println(Thread.currentThread().getName()
+ "The producer name is :++++++++++++++ " + this.name);
flag = true;
this.notify();
}
public synchronized void out(){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() +
"The consumer name is : ---------------------------" + this.name);
flag = false;
this.notify();
}
}
这是别人举的生产消息的一个例子,我的疑问是这里直接用if(flag)进行判断,flag的初始布尔值是从哪里弄的怎么进入到if里面去? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询