Java lock mechanism ReentrantLock

Performing assurance procedures commonly used in the lock ReentrantLock human sequence.

Write a function of class class simulation ReentrantLock

class MyLock {
     Private  Boolean Lock = to false ;
     Private  int holdCount = 0 ;
     Private Thread myThread = null ; // thread locks currently occupied 
    public  the synchronized  void Lock () { // thread synchronization allows only one thread to acquire the lock 
        Thread = Thread.currentThread currThread ();    // current access thread 
        IF (== Lock to true ! && currThread = myThread) {
             the try { 
                the wait (); 
            } the 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;
    }
}

When a thread can not get a lock will have to wait until the other thread releases the lock it will wake up, and then continue to get the run.

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 use

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

 

Guess you like

Origin www.cnblogs.com/chiweiming/p/11223127.html