黑马程序员技术交流社区

标题: 关于线程优化问题 [打印本页]

作者: 刘明月    时间: 2012-9-14 10:46
标题: 关于线程优化问题
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();
                        }
                }               
        }
}
为什么得到的结果还是不能实现同步(输入一个,输出一个)呢
作者: 李宁    时间: 2012-9-14 11:34
因为你用的不是同一个锁,就无法实现同步了,建议你用同步代码块试试
作者: 尤圣回    时间: 2012-9-14 11:39
你把this改成p看看
作者: 刘明月    时间: 2012-9-14 11:39
shaohuashang 发表于 2012-9-14 11:34
因为你用的不是同一个锁,就无法实现同步了,建议你用同步代码块试试

锁都是persons这个对象,在主函数调用时我只创建了一个该对象,所以锁应该是想同的
作者: 刘明月    时间: 2012-9-14 11:53
尤圣回 发表于 2012-9-14 11:39
你把this改成p看看

我是把同步写在了Person类里边了
作者: 尤圣回    时间: 2012-9-14 11:58
刘明月 发表于 2012-9-14 11:53
我是把同步写在了Person类里边了

你你就用类做锁对象




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2