可重入锁ReentrantLock打印H20

可重入锁ReentrantLock使用示例

这里只介绍Condition的等待通知机制,其他重入锁相关理论去参考其他的博客,首先ReentrantLock和synchronized都是同步锁,只不过前者需要手动释放,后者自动释放,所以在正常项目中后者使用偏多,但是想要更灵活的使用还是推荐ReentrantLock,如图一个lock可以创建多个Condition,每个Condition使用前需要获取lock

    static Lock lock = new ReentrantLock();
    static Condition H = lock.newCondition();
    static Condition TWO = lock.newCondition();
    static Condition O = lock.newCondition();

condition的方法和Thread的使用方法类似

Object condition
wait await
notify signal

下面实现一个三个线程每个线程分别打印H,2,O,使用等待唤醒机制实现

threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i =0 ;i<100 ;i++) {
                        lock.lock();
                        try {
                            if(x!=2){
                                try {
                                    O.await();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            x=0;
                            System.out.println("O");
                            H.signal();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }finally {
                            lock.unlock();
                        }
                    }
                }
            });


            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i =0;i<100;i++) {
                        lock.lock();
                        try {
                            if(x!=1){
                                try {
                                    TWO.await();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            x++;
                            System.out.print("2");
                            O.signal();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }finally {
                            lock.unlock();
                        }
                    }

                }
            });


            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 100; i++) {
                        lock.lock();
                        try {
                            if(x!=0){
                                try {
                                    H.await();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            x++;
                            System.out.print("H");
                            TWO.signal();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }finally {
                            lock.unlock();
                        }
                    }
                }
            });

        }

你可能感兴趣的:(锁)