毕老师的死锁例子

来源:互联网 发布:淘宝店铺怎么改名 编辑:程序博客网 时间:2024/06/10 14:58
package com.iflytek.bank;public class MainTest{        public static void main(String[] args)    {        new Thread(new Test(true)).start();        new Thread(new Test(false)).start();    }    }class Test implements Runnable{    private boolean flag;        public Test(boolean flag)    {        this.flag = flag;    }        @Override    public void run()    {        if (flag)        {            synchronized (Mylock.LOCKA)            {                System.out.println("----------if lockA");                synchronized (Mylock.LOCKB)                {                    System.out.println("----------------if lockB");                                    }            }        }        else        {            synchronized (Mylock.LOCKB)            {                System.out.println("**else lockB");                synchronized (Mylock.LOCKA)                {                    System.out.println("*******else lockA");                                    }            }        }            }}class Mylock{    public static Object LOCKA = new Object();        public static Object LOCKB = new Object();}





二、wait和notify用法

package com.iflytek.res;public class ProducerConsumer{    public static void main(String[] args)    {        Resucour resucour = new Resucour();        Producer producer = new Producer(resucour);        Consumer consumer = new Consumer(resucour);                Thread t = new Thread(producer);        Thread t1 = new Thread(producer);        Thread t2 = new Thread(consumer);        Thread t3 = new Thread(consumer);                t.start();        t1.start();        t2.start();        t3.start();            }}/** *  *对于多个生产者和消费者。 *为什么要定义while判断标记。 *原因是:让被唤醒的线程再一次判断标记 * * *为什么定义notifyAll; *因为需要唤醒对方线程 *因为只使用notify,容易出现幻醒本方法线程情况,导致程序中的所有的线程都处于等待 */class Resucour{    private String name;        private int count;        private boolean flag = false;        public synchronized void set(String name)    {                while (flag)            try            {                wait();            }            catch (InterruptedException e)            {                                // TODO Auto-generated catch block                e.printStackTrace();                            }        this.name = name + "-----" + count++;        System.out.println(Thread.currentThread().getName() + "生产了:" + this.name);        this.flag = true;        notifyAll();    }        public synchronized void out()    {        while (!flag)            //目的是让醒来的线程重新判断,,一旦用while循环,就有可能会造成所有线程处于等待状态,因此必须用notifyAll来唤醒            try            {                wait();            }            catch (InterruptedException e)            {                                // TODO Auto-generated catch block                e.printStackTrace();                            }        System.out.println(Thread.currentThread().getName() + "********************消费了:" + this.name);        this.flag = false;        notifyAll();    }}class Producer implements Runnable{    private Resucour resucour;        public Producer(Resucour resucour)    {        this.resucour = resucour;    }        @Override    public void run()    {        while (true)            resucour.set("面包");            }    }class Consumer implements Runnable{    private Resucour resucour;        public Consumer(Resucour resucour)    {        this.resucour = resucour;    }        @Override    public void run()    {        while (true)            resucour.out();    }}



0 0
原创粉丝点击