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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 18255007004 中级黑马   /  2015-1-31 17:21  /  777 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


线程间通信:其实就是多个线程在操作同一个资源,但操作的动作不同

例如
     --Input-->资源--Output--->
思考1:wait(),notify(),notify All(),用来操作线程为什么定义在Object类中?
1,这些方法存在与同步中
2,使用这些方法时必须要标识所属的同步锁
3,锁可以是任意对象,所以任意对象调用的方法一定定义Object类中

思考2:wait(),sleep(),有什么区别?
wait():释放资源,释放锁。
sleep:释放资源,不释放锁。
*/
//解决安全问题
class Res
{
        String name;
        String sex;
}
class Input implements Runnable
{
        private Res r;
        Input(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                int x = 0;
                while(true)
                {
                        synchronized(r)
                        {
                                if(x==0)
                                {
                                        r.name = "mike";
                                        r.sex = "man";
                                }
                                else
                                {
                                        r.name = "丽丽";
                                        r.sex = "女女女女女";
                                }       
                                x =(x+1)%2;
                        }               
                }
        }
}
class Output implements Runnable
{
        private Res r;
        Output(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                while(true)
                {
                        synchronized(r)
                        {
                                System.out.println(r.name+"---"+r.sex);
                        }

                }
        }
}
class  
{
        public static void main(String[] args)
        {
                System.out.println("Hello World!");
        }
}
//等待唤醒机制
class Res
{
        private String name;
        private String sex;
        private boolean flag = false;
        public synchronized void set(String name,String sex)
        {
                if(flag)
                        try{this.wait();}catch(Exception e){}
                this.name=name;
                this.sex=sex;
                flag = true;
                this.notify();
        }
        public synchronized void  out()
        {
                if(!flag)
                        try{this.wait();}catch(Exception e){}
                System.out.println(name+"---"+sex);
                flage = false;
                this.notify();
        }
}
class Input implements Runnable
{
        private Res r;
        Input(Res r)
        {
                this.r=r;
        }
        public void run()
        {
                int x = 0;
                while(true)
                {
                        if(x==0)
                        {
                                r.set("mike","man");
                        }
                        else
                        {
                                r.set("丽丽","女");
                        }       
                        x =(x+1)%2;                               
                }
        }
}
class Output implements Runnable
{
        private Res r;
        Output(Res r)
        {
                this.r = r;
        }
        public void run()
        {
                while(true)
                {
                                r.out();
                }
        }
}
class InputOutputDemo
{
        public static void main(String[] args)
        {
                Res r = new Res();
                new Thread (new Input(r)).start();
                new Thread (new Output(r)).start();
        /*        Input in = new Input(r);
                Output out = new Output(r);

                Thread t1 = new Thread(in);
                Thread t2 = new Thread(out);

                t1.start();
                t2.start();
        */
        }
}
/*
wait();
notify();
notifyAll();
都使用在同步中,因为要对持有监视器(锁)的线程操作。
所以要使用在同步中,因为只有同步才具有锁。
为什么这些操作线程的方法要定义Object类中呢?
因为这些方法在操作同步中线程时,都必须要标识它们所操作线程持有的锁,
只有同一个锁上的被等待的线程,才可以被同一个锁上的notify唤醒
不可以对不同锁中的线程进行唤醒
也就是说,等待和唤醒必须是同一个锁。而锁可以是任意对象,所以可以被任意对象调用的方法定义在Object类中
notify();存放在线程池中,只有存放在,线程池中的线程才能被唤醒,
                                //通常唤醒第一个被等待的线程

*/
/*
生产者消费者
对于多个生产者和消费者
  为什么要定义while判断标记
  原因:让被唤醒的线程再判断标记。
为什么定义notifyAll,
  因为需要唤醒对方线程
  因为只用notify,容易出现只唤醒本方线程的情况,
  导致程序中的所有线程都等待。
*/
class ProducerConsumerDemo
{
        public static void main(String[] args)
        {
                Resource r = new Resource();
                Producer pro = new Producer(r);
                Consumer con = new Consumer(r);
                Thread t1 = new Thread(pro);
                Thread t2 = new Thread(con);
                t1.start();
                t2.start();
        }
}
class Resourse
{
        private String name;
        private int count =1;
        private boolean flag =false;
        public synchronized void set(String name)
        {
                while(flag)
                        try{this.wait();}catch(Exception e){}
                this.name = name +"--"+ count++;
                System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
                flag = true;
                this.notifyAll();
        }
        public synchronized void out()
        {
                while(!flag)
                        try{this.wait();}catch(Exception e){}
                System.out.println(Thread.currentThread().getName()+"...消费者.."+this.name);
                flag = false;
                this.notifyAll();
        }
}
class Producer implements Runnable
{
        private Resource res;
        Producer(Resource res)
        {
                this.res = res;
        }
        public void run()
        {
                while(true)
                {
                        res.set("++商品++");
                }
        }
}
class Consumer implements Runnable
{
        private Resource res;
        Consumer(Resource res)
        {
                this.res = res;
        }
        public void run()
        {
                while(true)
                {
                        res.out();
                }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马