if else 判断不走分支的问题

明月镇魂 2017-03-12 10:08:48
自己在做练习的时候发现我写的if else语句不走分支,语句如下
public class ThreadTest6 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person_test6 pt = new Person_test6();
Input ip = new Input(pt);
Output op = new Output(pt);
Thread it = new Thread(ip);
Thread ot = new Thread(op);
it.start();
ot.start();

}

}

class Person_test6 {
private String Name;
private int Age;
private int x;
private boolean flag = false;

public synchronized void InputPerson() {
// 输入方法
while (true) {
if (flag == true) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
if (x == 1) {
this.Name = "Mark";
this.Age = 25;
System.out.println("mark is ready");
} else {
this.Name = "Rose";
this.Age = 22;
System.out.println("rose is ready");
}
}

x = (x + 1) % 2;
System.out.println("x=" + x);
flag = true;
System.out.println(flag);
this.notify();
}
}

public synchronized void OutputPerson() {
// 输出方法
while (true) {
if (flag == false) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
System.out.println(Name + "******" + Age);
flag = false;
System.out.println(flag);
this.notify();
}

}
}

class Input implements Runnable {
// 输入线程
private Person_test6 pt;

Input(Person_test6 pt) {
this.pt = pt;
}

public void run() {
while (true)
pt.InputPerson();
}
}

class Output implements Runnable {
// 输出线程
private Person_test6 pt;

Output(Person_test6 pt) {
this.pt = pt;
}

public void run() {
while (true)
pt.OutputPerson();
}
}


而且比较奇怪的是第一个两个方法的else加与不加,第一个方法的else加不加大括号,最终出来的结果都不一样,希望能解答一下。
...全文
547 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhujinqiang 2017-03-14
  • 打赏
  • 举报
回复
所以可以归结为代码书写的不规范吗?
自由自在_Yu 2017-03-13
  • 打赏
  • 举报
回复
希望能说的清楚一点
明月镇魂 2017-03-13
  • 打赏
  • 举报
回复
引用 6 楼 zs808 的回复:
能删评论么,,,,,我真是一本正经的胡说八道啊。。。 请楼主忽略以上我的两个回答。。。 你这个问题的根本原因在于
public synchronized void OutputPerson() {
        // 输出方法
        while (true) {
            if (flag == false) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else
                System.out.println(Name + "******" + Age);
            flag = false;
            System.out.println(flag);
            this.notify();
        }
 
    }
这里的
flag = false;
            System.out.println(flag);
            this.notify();
没有写在else里,放else里就行了
 else{
                System.out.println(Name + "******" + Age);
	            flag = false;
	            System.out.println(flag);
	            this.notify();
            }
简写的ifelse害死人啊。。。。。
谢谢回复,确实是这样,今天加了一些断点进去,发现OutputPerson()和InputPerson()两个方法else要么不加,要么将下面的所有语句都包含到else语句块中,另外我还发现一个有趣的情况,同一个线程会连续两次拿到锁,如果不是因为这样,我或许还不能发现这个问题。
qq_36778080 2017-03-13
  • 打赏
  • 举报
回复
有else{}的属于if else的代码块 没有else或者没有{}的是不属于if else的代码块 无论有没有if都会走{}内的代码 {...}称为对象代码块 是否使用else看流程需要
zhujinqiang 2017-03-13
  • 打赏
  • 举报
回复
原来是少了一格{号啊
zs808 2017-03-13
  • 打赏
  • 举报
回复
能删评论么,,,,,我真是一本正经的胡说八道啊。。。 请楼主忽略以上我的两个回答。。。 你这个问题的根本原因在于
public synchronized void OutputPerson() {
        // 输出方法
        while (true) {
            if (flag == false) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else
                System.out.println(Name + "******" + Age);
            flag = false;
            System.out.println(flag);
            this.notify();
        }
 
    }
这里的
flag = false;
            System.out.println(flag);
            this.notify();
没有写在else里,放else里就行了
 else{
                System.out.println(Name + "******" + Age);
	            flag = false;
	            System.out.println(flag);
	            this.notify();
            }
简写的ifelse害死人啊。。。。。
zs808 2017-03-13
  • 打赏
  • 举报
回复
不好意思,回答错了,wait是会释放对象锁,不会造成互斥条件。 你的问题出现的根源是在于flag=true与notify应该在else里。否则有可能会出现两个线程同时wait而不nitofy的情况。 以下是修正的代码:
package org.imzhs.learn.test;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args){
		 // TODO Auto-generated method stub
        Person_test6 pt = new Person_test6();
        Input ip = new Input(pt);
        Output op = new Output(pt);
        Thread it = new Thread(ip);
        Thread ot = new Thread(op);
        it.start();
        ot.start();
	}
}

class Person_test6 {
    private String Name;
    private int Age;
    private int x;
    private boolean flag = false;
 
    public synchronized void InputPerson() {
        // 输入方法
        while (true) {
            if (flag == true) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                if (x == 1) {
                    this.Name = "Mark";
                    this.Age = 25;
                    System.out.println("mark is ready");
                } else {
                    this.Name = "Rose";
                    this.Age = 22;
                    System.out.println("rose is ready");
                }
                //以下提到else里
                x = (x + 1) % 2;
                System.out.println("x=" + x);
                flag = true;
                System.out.println(flag);
                this.notify();
            }
        }
    }
 
    public synchronized void OutputPerson() {
        // 输出方法
        while (true) {
            if (flag == false) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else
            {
                //以下提到else里
                System.out.println(Name + "******" + Age);
                flag = false;
                System.out.println(flag);
                this.notify();
            }
        }
 
    }
}
 
class Input implements Runnable {
    // 输入线程
    private Person_test6 pt;
 
    Input(Person_test6 pt) {
        this.pt = pt;
    }
 
    public void run() {
        while (true)
            pt.InputPerson();
    }
}
 
class Output implements Runnable {
    // 输出线程
    private Person_test6 pt;
 
    Output(Person_test6 pt) {
        this.pt = pt;
    }
 
    public void run() {
        while (true)
            pt.OutputPerson();
    }
}
zs808 2017-03-13
  • 打赏
  • 举报
回复
因为你在不同的线程里面用到了同一个对象pt,而pt里面的方法都是synchronized,这就导致这两个方法的执行在所有线程上是互斥的,然而,你在这两个方法里面又写了一个没有break或者return的while(true),这就导致A线程执行某个方法的时候,其它线程都在等,自然,flag也不会被修改,然后,线程死锁。
zhujinqiang 2017-03-13
  • 打赏
  • 举报
回复
if里面的条件有没有写错?
ryuugu_rena 2017-03-13
  • 打赏
  • 举报
回复
if else不走else是因为走if了,然后就不走else了。

62,614

社区成员

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

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