A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘明月 中级黑马   /  2012-9-14 10:46  /  1392 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package p1;
public class Persons {
        private String name;
        private String sex;
        private boolean flag;
        public synchronized void setName(String name,String sex) throws InterruptedException
        {
                if(flag)
                {
                this.name=name;
                this.sex=sex;
                flag=false;
                this.notify();
                }
                else
                        this.wait();
               
        }
        public synchronized void show() throws InterruptedException
        {
                if(!flag)
                {
                        System.out.println(name+"     "+sex);
                        flag=true;
                        this.notify();
                }
                else
                {
                        this.wait();
                }
        }
}
public class You {
        public static void main(String[] args) {
                Persons p=new Persons();
                Thread t1=new Thread(new One(p));
                Thread t2=new Thread(new Two(p));
                t1.start();
                t2.start();
        }
}

class One implements Runnable
{
        Persons p;
        One(Persons p)
        {
                this.p=p;
        }
        int n=0;
        public void run()
        {
                while(true)
                {                       
                                if(n==0)
                                {                       
                                        try {
                                                p.setName("li", "nann");
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
                                }
                                else
                                {
                                        try {
                                                p.setName("往往冯绍峰", "的份上辐射防护");
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                        }
                                }
                                n=(n+1)%2;
                }
               
        }
}
class Two implements Runnable
{
        Persons p;
        Two(Persons p)
        {
                this.p=p;
        }
        public void run()
        {
                while(true)
                {               
                        try {
                                p.show();
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }               
        }
}
为什么得到的结果还是不能实现同步(输入一个,输出一个)呢

5 个回复

倒序浏览
因为你用的不是同一个锁,就无法实现同步了,建议你用同步代码块试试
回复 使用道具 举报
你把this改成p看看
回复 使用道具 举报
shaohuashang 发表于 2012-9-14 11:34
因为你用的不是同一个锁,就无法实现同步了,建议你用同步代码块试试

锁都是persons这个对象,在主函数调用时我只创建了一个该对象,所以锁应该是想同的
回复 使用道具 举报
尤圣回 发表于 2012-9-14 11:39
你把this改成p看看

我是把同步写在了Person类里边了
回复 使用道具 举报
刘明月 发表于 2012-9-14 11:53
我是把同步写在了Person类里边了

你你就用类做锁对象
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马