实现死锁和生产者消费者

死锁程序:

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();
            }
        }
    }   
}

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

你可能感兴趣的:(java)