zoukankan      html  css  js  c++  java
  • Java锁机制ReentrantLock

    ReentrantLock 锁常用于保证程序的人为顺序执行。

    写一个类模拟ReentrantLock类的功能

    class MyLock{
        private boolean lock = false;
        private int holdCount = 0;
        private Thread myThread = null; //当前占用锁的线程
        public synchronized void lock(){ //线程同步,只允许一个线程来获取这把锁
            Thread currThread = Thread.currentThread();   //当前访问线程
            if(lock == true && currThread != myThread){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //给予锁
            myThread = currThread;
            lock = true;
            holdCount ++;
        }
        public synchronized  void unlock(){
            Thread currThread = Thread.currentThread();   //当前访问线程
            if(currThread == myThread && holdCount>0){
                holdCount -- ;
                if(holdCount == 0){
                    notifyAll();
                    lock = false;
                    myThread = null;
                }
            }
        }
    
        public int getHoldCount() {
            return holdCount;
        }
    }

    当一个线程不能得到一把锁时就会产生等待,等到其他线程释放锁后其将被唤醒,然后拿到所继续运行。

    public class Test {
        public static void main(String[] args) {
            MyLock myLock = new MyLock();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myLock.lock();
                    System.out.println(Thread.currentThread().getName()+"得到锁:"+myLock.getHoldCount());
                    myLock.lock();  //可重入锁
                    System.out.println(Thread.currentThread().getName()+"重入成功:"+myLock.getHoldCount());
                    myLock.unlock();
                    System.out.println(Thread.currentThread().getName()+"释放锁:"+myLock.getHoldCount());
                    myLock.unlock();
                    System.out.println(Thread.currentThread().getName()+"释放锁:"+myLock.getHoldCount());
                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    myLock.lock();
                    System.out.println(Thread.currentThread().getName()+"得到锁:"+myLock.getHoldCount());
                    myLock.unlock();
                    System.out.println(Thread.currentThread().getName()+"释放锁:"+myLock.getHoldCount());
                }
            }).start();
        }
    }

    ReentrantLock的使用

    public class LockTest {
        ReentrantLock lock = new ReentrantLock();
        public void a() throws InterruptedException {
            lock.lock();
            System.out.println(lock.getHoldCount());
            dosomething();
            lock.unlock();
            System.out.println(lock.getHoldCount());
        }
        public void dosomething() throws InterruptedException {
            lock.lock();
            System.out.println(lock.getHoldCount());
            lock.unlock();
            System.out.println(lock.getHoldCount());
        }
        public static void main(String[] args) throws InterruptedException {
            LockTest lockTest = new LockTest();
            lockTest.a();
        }
    }
  • 相关阅读:
    Apache Hadoop 英文官方参考文档及中文文档
    谷歌大数据那三篇论文-中文版
    Java学习笔记(一):基础概念和语法
    Java基础概念、知识点整理
    TensorFlow基础知识
    Kafka集群环境配置
    Sqoop数据迁移工具的使用
    HBase的安装和使用
    Flume日志采集框架的使用
    zookeeper的安装和使用
  • 原文地址:https://www.cnblogs.com/chiweiming/p/11223127.html
Copyright © 2011-2022 走看看