黑马程序员技术交流社区
标题:
语句理解,求大神,
[打印本页]
作者:
菜鸟征程
时间:
2015-9-12 23:10
标题:
语句理解,求大神,
class Resource{
private String name ;
private String sex ;
private boolean flag = false;
public synchronized void set(String name,String sex){
if(flag )
try{
this.wait();
} catch(InterruptedException e){
e.printStackTrace();
}
this.name = name;
this.sex = sex;
flag = true ;
this.notify();
}
public synchronized void out(){
if(!flag )
try{
this.wait();
} catch(InterruptedException e){
e.printStackTrace();
}
System. out.println(name + "..." + sex);
flag = false ;
this.notify();
}
}
//输入
class Input implements Runnable{
Resource r;
Input(Resource r){
this.r = r;
}
public void run(){
int x = 0;
while(true ){
if(x == 0){
r.set( "mike","男" );
} else{
r.set( "lili","女" );
}
x = (x + 1)%2;
}
}
}
//输出
class Output implements Runnable{
Resource r;
Output(Resource r){
this.r = r;
}
public void run(){
while(true ){
r.out();
}
}
}
class ResourceDemo {
public static void main(String[] args){
//创建资源
Resource r = new Resource();
//创建任务
Input in = new Input(r);
Output out = new Output(r);
//创建线程,执行路径
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
//开启线程
t1.start();
t2.start();
}
}
if(flag)和if(!flag)这两句是不是如果false就执行下面的 和 如果非false就执行下面的。
作者:
xiaoxiao147
时间:
2015-9-12 23:14
if(flag){A} B ,flag=true,执行A,否则。执行B.
作者:
疯疯疯疯疯子
时间:
2015-9-13 00:01
路过,看不懂,帮顶
作者:
MilesMatheson
时间:
2015-9-13 01:11
if(flag) 中看flag,如果为真进入if,否则跳过;
if(!flag) 中看!flag, !falg的值过为真则进入if,否则跳过;
其实就是看if的括号里面的boolean表达式的值来判断是否进入if.
作者:
菜鸟征程
时间:
2015-9-13 10:55
感谢各位大神。
作者:
插兜
时间:
2015-9-13 12:06
看不懂,不过写这么长,赞一下
作者:
boboyuwu
时间:
2015-9-13 17:24
对的就是这样理解的。。。。。。。。
作者:
霹雳三口组
时间:
2015-9-13 21:49
那要看flag的初始值了,如果flag初始值是true,则if(flag)判断为true,执行下面代码块,如果flag初始值是false,则if(!flag)判断为true,执行下面代码块
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2