锁的应用以及实现生产消费模型

1.启动三个线程,打印递增的数字,线程1打印1,2,3,4,5.线程2打印6,7,8,9,10,线程3打印11,12,13,14,15.接着再由线程1打印16,17,18,19,20,以此类推,直到打印到75

public class Test1 implements Runnable {

    public static int outInt = 1;

    public Integer id;

    public Test1(Integer id) {
        this.id = id;
    }

    public static Object lock = new Object();

    @Override
    public void run() {
        synchronized (lock) {
            while (true) {
                while ((outInt - 1) / 5 % 3 + 1 != id) {
                    try {
                        lock.wait();
                        if (outInt > 75) {
                            lock.notifyAll();
                            return;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println("线程" + id + ":" + outInt++);
                lock.notifyAll();

            }
        }
    }


    public static void main(String[] args) {
        Thread t1 = new Thread(new Test1(1));
        Thread t2 = new Thread(new Test1(2));
        Thread t3 = new Thread(new Test1(3));

        t1.start();
        t2.start();
        t3.start();


    }
}

2.单一的生产消费模型(多生产消费模型在下面):

public class Test2 {

    public static void main(String[] args) {
        final BlokingQueue<String> blokingQueue = new BlokingQueue<>();

        Thread t1 = new Thread(() -> {
            while (true) {
                try {
                    System.out.println(blokingQueue.get());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            int i = 0;
            while (true) {
                blokingQueue.add("ABC" + i++);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();
    }
}


class BlokingQueue<T> {
    private LinkedList<T> linkedList = new LinkedList<>();

    public synchronized T get() throws InterruptedException {
        while (linkedList.size() == 0) {
            wait();
        }
        T t = linkedList.getFirst();
        linkedList.removeFirst();
        return t;
    }

    public synchronized void add(T t) {
        linkedList.add(t);
        notifyAll();
    }
}

3.多生产者消费者模型

public class Test3 {
    public static void main(String[] args) {

        ArrayBlokingQueue<String> arrayBlockingQueue = new ArrayBlokingQueue<>(20);
        List<Runnable> adds = new ArrayList<>();
        List<Runnable> gets = new ArrayList<>();

        adds.add(new GetThread(arrayBlockingQueue));
        adds.add(new GetThread(arrayBlockingQueue));
        adds.add(new GetThread(arrayBlockingQueue));

        gets.add(new AddThread(arrayBlockingQueue));
        gets.add(new AddThread(arrayBlockingQueue));
        gets.add(new AddThread(arrayBlockingQueue));


        for (int i = 0; i < adds.size(); i++) {
            new Thread(adds.get(i), "" + i).start();
        }

        for (int i = 0; i < gets.size(); i++) {
            new Thread(gets.get(i), "" + i).start();
        }


    }

}

/**
 * 生产消费队列阻塞
 * @param <T>
 */
class ArrayBlokingQueue<T> {
    private LinkedList<T> linkedList = new LinkedList<>();

    private Integer queueSize;

    private Lock lock = new ReentrantLock();

    private Condition getCon = lock.newCondition();
    private Condition addCon = lock.newCondition();


    public ArrayBlokingQueue(Integer queueSize) {
        this.queueSize = queueSize;
    }

    public T get() {
        lock.lock();
        while (linkedList.size() == 0) {
            System.out.println("__________________________________________队列空");
            try {
                getCon.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            T t = linkedList.getFirst();
            linkedList.removeFirst();
            return t;
        } finally {
            addCon.signalAll();
            lock.unlock();
        }

    }

    public void add(T t) {
        lock.lock();
        while (linkedList.size() == queueSize) {
            System.out.println("-------------------------------------------------------------队列满");
            try {
                addCon.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            linkedList.add(t);

        } finally {
            getCon.signalAll();
            lock.unlock();
        }


    }

}

/**
 * 消费者
 */
class GetThread implements Runnable {
    private ArrayBlokingQueue<String> arrayBlokingQueue;

    public GetThread(ArrayBlokingQueue<String> arrayBlokingQueue) {
        this.arrayBlokingQueue = arrayBlokingQueue;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("消费者" + Thread.currentThread().getName() + ":" + arrayBlokingQueue.get());
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

/**
 * 生产者
 */
class AddThread implements Runnable {
    private ArrayBlokingQueue<String> arrayBlokingQueue;

    public AddThread(ArrayBlokingQueue<String> arrayBlokingQueue) {
        this.arrayBlokingQueue = arrayBlokingQueue;
    }

    @Override
    public void run() {
        for (int i = 0; ; i++) {
            arrayBlokingQueue.add("生产者" + Thread.currentThread().getName() + "的" + i);
            System.out.println("生产者" + Thread.currentThread().getName() + ":" + i);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值