美文网首页
多线程8实现重入锁

多线程8实现重入锁

作者: RyanHugo | 来源:发表于2020-03-22 11:46 被阅读0次

非重入锁

计数

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

public class MyLockUnenter implements Lock{

    private boolean isLock = false;
    
    @Override
    public synchronized void lock() {
        // TODO Auto-generated method stub
        while (isLock) {
            try {
                wait();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        isLock = true;
    }
    
    @Override
    public synchronized void unlock() {
        // TODO Auto-generated method stub
        isLock = false;
        notify();
    }
    
    @Override
    public void lockInterruptibly() throws InterruptedException {
        // TODO Auto-generated method stub  
    }
    @Override
    public boolean tryLock() {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public boolean tryLock(long time, TimeUnit unit)
            throws InterruptedException {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public Condition newCondition() {
        // TODO Auto-generated method stub
        return null;
    }
}

public class TestUnenter {

    Lock lock = new MyLockUnenter();
    private int value;
    private int getnum(){
        lock.lock();
        int a = value++;
        lock.unlock();
        return a;
    }
    
    public static void main(String[] args) {
        
        TestUnenter testUnenter = new TestUnenter();
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (true) {
                    System.out.println(Thread.currentThread().getName()+" "+testUnenter.getnum());
                }
            }
        }).start();
        
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (true) {
                    System.out.println(Thread.currentThread().getName()+" "+testUnenter.getnum());
                }
            }
        }).start();
        
    }
}

线程无限期等待示例

public class TestUnenter {

    Lock lock = new MyLockUnenter();
    public void a(){
        lock.lock();
        System.err.println("a");
        b();
        lock.unlock();
    }
    
    public void b(){
        lock.lock();
        System.err.println("b");
//      b();
        lock.unlock();
    }
    
    public static void main(String[] args) {
        
        TestUnenter testUnenter = new TestUnenter();
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                TestUnenter d = new TestUnenter();
                d.a();
            }
        }).start();     
    }
}

重入锁,类似于sychronized,不会有死锁情况

public class MyLock implements Lock{

    private boolean isLock = false;
    private Thread lockby = null;//被锁住的线程
    private int locknum ;//锁重入的次数
    
    @Override
    public synchronized void lock() {
        // TODO Auto-generated method stub
//      先判断是否有线程已经获得锁,判断当前线程是否为锁住的线程
        Thread current = Thread.currentThread();
        while (isLock&&current!=lockby) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        lockby = current;
        isLock = true;
        locknum++;
    }
    
    @Override
    public synchronized void unlock() {
        // TODO Auto-generated method stub
//      判断当前线程是否为锁住的线程
        if (lockby == Thread.currentThread()) {
//          重入次数减去1
            locknum--;
//          当最后一个锁被释放
            if (locknum == 0) {
                isLock = false;
                notify();
            }
        }
        
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public boolean tryLock() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit)
            throws InterruptedException {
        // TODO Auto-generated method stub
        return false;
    }

    

    @Override
    public Condition newCondition() {
        // TODO Auto-generated method stub
        return null;
    }

}

相关文章

  • 多线程8实现重入锁

    非重入锁 计数 线程无限期等待示例 重入锁,类似于sychronized,不会有死锁情况

  • Java并发编程-重入锁

    章节目录 什么是重入锁 底层实现-如何实现重入 公平与非公平获取锁的区别与底层实现 1.什么是重入锁 1.1 重入...

  • 2018-03-01

    随记 1.多线程锁机制之ReentrankLock可重入锁2.RabbitMQ实现消息优先级机制 概况 粗略理解为...

  • java.util.concurrent.locks.Reent

    什么是重入锁 重入锁就是指重复进入锁,它表示该锁能够支持一个线程对资源的重复加锁。 重入锁继承与实现关系 重入锁的...

  • Java并发 | ReentrantLock类

    ReentrantLock可重入锁 在Java多线程中,可以使用synchronized关键字来实现线程之间同步互...

  • J.U.C-AQS-ReentrantLock

    ReentrantLock(可重入锁)和synchronized区别 可重入性 锁的实现(ReentrantLoc...

  • 死磕 java同步系列之ReentrantLock源码解析(一)

    问题 (1)重入锁是什么? (2)ReentrantLock如何实现重入锁? (3)ReentrantLock为什...

  • golang可重入锁的实现

    如何实现可重入锁?实现一个可重入锁需要这两点:1.记住持有锁的线程2.统计重入的次数 转自golangroadma...

  • java可重入锁

    可重入概念: java的可重入锁: 可重入锁的一种实现方式: 可重入锁的两种使用例子: 例子1: 例子2: 例子1...

  • ReentrantReadWriteLock代码浅析

    介绍 除了重入锁ReentrantLock以外,Doug Lea大神还顺带实现了读写重入锁ReentrantRea...

网友评论

      本文标题:多线程8实现重入锁

      本文链接:https://www.haomeiwen.com/subject/uhmmyhtx.html