解决方案 »

  1.   

    哪里死锁了吗  两个线程在跑  info类有标志位判断 唤醒等待 应该会有输出啊
      

  2.   

    可以说一些 为什么要使用super调用notify吗
    是在说 这是在唤醒基类的锁
      

  3.   

    还是在说
    这是在访问被覆盖的notify
    子类已经重写了他
    即便 notify是 final
    真的好厉害
      

  4.   

    条件不对public class Info {

    private String name = "谢龙涛";
    private String hobby = "篮球";
    private boolean flag = false;

    boolean key=true;

    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getHobby() {
    return hobby;
    }
    public void setHobby(String hobby) {
    this.hobby = hobby;

    public  synchronized void  set(String name,String hobby){
    System.out.println(Thread.currentThread().getName()+"set");
    try {

    while(key)
    this.wait();
    } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    this.setName(name);
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    this.setHobby(hobby);
    key=true;
    super.notify();
    }
    public synchronized void get(){
    System.out.println(Thread.currentThread().getName()+"get");
    try {
    while(!key)
    this.wait();
    } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("name:" + this.getName() + " hobby:"
    + this.getHobby());
    key= false;
    super.notify();
    }
    }
      

  5.   

    楼主是初学Java吧,你分享的这个问题说明你是一个爱思考的学习者,多线程通信原本也是Java中比较难的一个地方,lz的Info代码有点问题,我做了简单的修改,希望能帮助lz明白,欢迎给我留言哦:
    /**
     * 
     */
    package com.meritit.dm.csdn.thread;import java.util.concurrent.TimeUnit;public class Info {
    private String name = "谢龙涛";
    private String hobby = "篮球";
    private boolean flag; public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public String getHobby() {
    return hobby;
    } public void setHobby(String hobby) {
    this.hobby = hobby;
    } public synchronized void set(String name, String hobby) {
    if (!flag) {
    try {
    wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }
    setName(name);
    setHobby(hobby);
    System.out.println(Thread.currentThread().getName() + " set[" + name
    + "," + hobby + "]");
    try {
    TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    flag = false;
    notify();
    } public synchronized void get() {
    if (flag) {
    try {
    wait();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }
    System.out.println(Thread.currentThread().getName() + " get[" + name
    + "," + hobby + "]");
    try {
    TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    flag = true;
    notify();
    }
    }