实现死锁和生产者消费者

死锁程序:

public class TestDeadLock implements Runnable {

    public int flag;
    static Object o1 = new Object();
    static Object o2 = new Object();

    public void run() {
        System.out.println("flag=" + flag);
        if(flag == 1) {
            synchronized (o1) {
                System.out.println("flag"+flag+" I have gotten o1");
                synchronized (o2) {
                    System.out.println("flag"+flag+"done");
                }
            }
        }else{
            synchronized (o2) {
                System.out.println("flag"+flag+"I have gotten o2");
                synchronized (o1) {
                    System.out.println("flag"+flag+"done");
                }
            }
        }
    }

    public static void main(String[] args) {
        TestDeadLock test1 = new TestDeadLock();
        TestDeadLock test2 = new TestDeadLock();
        test1.flag = 1;
        test2.flag = 0;
        Thread t1 = new Thread(test1);
        Thread t2 = new Thread(test2);
        t1.start();
        t2.start();
    }
}

运行结果:
flag=0
flag=1
flag1 I have gotten o1
flag0I have gotten o2

生产者消费者程序:
用wait和notify,synchronized来实现

package studyJavaSE;

import javax.net.ssl.SSLContext;

public class ProducerConsumer {
    public static void main(String[] args) {
        SyncStock ss = new SyncStock();
        Producer producer = new Producer(ss);
        Consumer consumer = new Consumer(ss);
        new Thread(producer).start();
        new Thread(consumer).start();
    }
}

//面包
class Bread {
    int id;
    public Bread(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "Bread: " + id;
    }
}

//空盘子
class SyncStock {
    int index = 0;
    Bread[] breads = new Bread[6]; //可放6个面包

    public synchronized void push(Bread bread) { //synchronized加锁,每次只能一个线程进
        while (index == breads.length) { //盘子满了就要等别人消费
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        breads[index] = bread;
        index ++;
    }

    public synchronized Bread pop() {
        while(index == 0) {   //盘子空了就要等生产
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        index--;
        return breads[index];
    }
}

//生产者
class Producer implements Runnable {
    SyncStock ss = null;
    public Producer(SyncStock ss) {
        this.ss = ss;
    }

    @Override
    public void run() {
        for(int i=0;i<20;i++) {
            Bread bread = new Bread(i);
            ss.push(bread);
            System.out.println("生产了" + bread);
            try {
                Thread.sleep((int)Math.random() + 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

//消费者
class Consumer implements Runnable {
    SyncStock ss = null;
    public Consumer(SyncStock ss) {
        this.ss = ss;
    }
    @Override
    public void run() {
        for(int i=0;i<20;i++) {
            Bread bread = ss.pop();
            System.out.println("消费了" + bread);
            try {
                Thread.sleep((int)Math.random() );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }   
}

用两个线程分别跑消费者生产者,可以看到即使生产者生产得很慢,但是消费者还是要等他。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值