ReentrantLock

 创建一个显示锁

package com.dwz.locks;

import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

public class ReentrantLockExample {
    private static final Lock lock = new ReentrantLock();
    
    public static void main(String[] args) throws InterruptedException {
        IntStream.range(0, 2).forEach(i -> new Thread() {
            @Override
            public void run() {
                needLock();
            }
        }.start());
    }
    
    public static void needLock() {
        try {
            lock.lock();
            Optional.of("The thread-" + Thread.currentThread().getName() + " get lock and will do working.").ifPresent(System.out::println);
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
    //needLock等价于needLockBySync
    public static void needLockBySync() {
        synchronized(ReentrantLockExample.class) {
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

测试锁可以被打断:lock.lockInterruptibly();

package com.dwz.locks;

import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

public class ReentrantLockExample {
    private static final Lock lock = new ReentrantLock();
    
    public static void main(String[] args) throws InterruptedException {
        
        Thread thread1 = new Thread(() -> testUnInterruptibly());
        thread1.start();
        
        TimeUnit.SECONDS.sleep(1);
        
        Thread thread2 = new Thread(() -> testUnInterruptibly());
        thread2.start();
        
        TimeUnit.SECONDS.sleep(1);
        
        thread2.interrupt();
        System.out.println("=================");

    }
    public static void testUnInterruptibly() {
        try {
            lock.lockInterruptibly();
            Optional.of("The thread-" + Thread.currentThread().getName() + " get lock and will do working.").ifPresent(System.out::println);
            while(true) {
                
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

拿不到锁就算了:lock.tryLock()

package com.dwz.locks;

import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

public class ReentrantLockExample {
    private static final Lock lock = new ReentrantLock();
    
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> testTryLock());
        thread1.start();
        
        TimeUnit.SECONDS.sleep(1);
        
        Thread thread2 = new Thread(() -> testTryLock());
        thread2.start();
    }
    
    //拿不到锁就算了
    public static void testTryLock() {
        if(lock.tryLock()) {
            try {
                Optional.of("The thread-" + Thread.currentThread().getName() + " get lock and will do working.").ifPresent(System.out::println);
                while(true) {
                    
                }
            } finally {
                lock.unlock();
            }
        } else {
            Optional.of("The thread-" + Thread.currentThread().getName() + " not get lock.").ifPresent(System.out::println);
        }
    }
}

 测试ReentrantLock的getQueueLength()、hasQueuedThreads()、getHoldCount()方法

package com.dwz.locks;

import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockExample2 {
    private static final ReentrantLock lock = new ReentrantLock();
    
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> testUnInterruptibly());
        thread1.start();
        
        TimeUnit.SECONDS.sleep(1);
        
        Thread thread2 = new Thread(() -> testUnInterruptibly());
        thread2.start();
        
        TimeUnit.SECONDS.sleep(1);
        Optional.of(lock.getQueueLength()).ifPresent(System.out::println);
        Optional.of(lock.hasQueuedThreads()).ifPresent(System.out::println);
    }
    
    public static void testUnInterruptibly() {
        try {
            lock.lockInterruptibly();
            Optional.of(Thread.currentThread().getName() + ":" + lock.getHoldCount()).ifPresent(System.out::println);
            Optional.of("The thread-" + Thread.currentThread().getName() + " get lock and will do working.").ifPresent(System.out::println);
            while(true) {
                
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
原文地址:https://www.cnblogs.com/zheaven/p/13360130.html