Basic operation of display lock
ReentrantLock reentrant lock
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockThread extends Thread{
private Lock lock;
private static int num=0;
public LockThread(Lock lock) {
this.lock=lock;
}
@Override
public void run() {
for(int i=0;i<100000;i++){
try {
lock.lock();
lock.lock();
num++;
}finally {
lock.unlock();
lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
Lock lock = new ReentrantLock();
LockThread lockThread1 = new LockThread(lock);
LockThread lockThread2 = new LockThread(lock);
lockThread1.start();
lockThread2.start();
lockThread1.join();
lockThread2.join();
System.out.println(LockThread.num);
}
}
lock.lock() is generally written in a try code block, lock.unlock() is written in a finally code block, Lock is an abstract class, ReentrantLock() also has reentrancy, ReentrantLock(true) can guarantee the fairness of threads Sex (synchronized is an unfair lock, and the thread is awakened randomly when the thread is awakened)
lockInterruptibly
lock.lockInterruptibly()The current thread will get the lock without interruption, if the thread is interrupted, an error will be reported
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockThread extends Thread{
private Lock lock;
public LockThread(Lock lock) {
this.lock=lock;
}
@Override
public void run() {
try {
lock.lockInterruptibly();
System.out.println(Thread.currentThread().getName()+"线程获得锁");
sleep(1000);
lock.unlock();
System.out.println(Thread.currentThread().getName()+"线程释放");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
public static void main(String[] args) throws InterruptedException {
Lock lock = new ReentrantLock();
LockThread lockThread1 = new LockThread(lock);
LockThread lockThread2 = new LockThread(lock);
lockThread1.start();
sleep(50);//保证lockThread1获得锁
lockThread2.start();
sleep(50);
lockThread2.interrupt();//中断lockThread2线程
}
}
When lockThread1 obtains the lock, the lockThread2 thread lock.lockInterruptibly() may report an error after the lockThread2 thread runs.
isHeldByCurrentThread
The isHeldByCurrentThread method in ReentrantLock: whether the current thread holds the lock
tryLock(long time, TimeUnit unit)
lock.tryLock(long time, TimeUnit unit) is'locked' by no other thread for a given period of time, and the current thread is not interrupted to obtain the lock. Return true if the lock is acquired, false if the lock is not acquired, and the tryLock() method without parameters will not wait. If the lock is used by other threads, it will return false.
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockThread extends Thread{
private ReentrantLock lock;
public LockThread(ReentrantLock lock) {
this.lock=lock;
}
@Override
public void run() {
try {
if( lock.tryLock(3, TimeUnit.SECONDS)){
//等待3秒
System.out.println(Thread.currentThread().getName()+"线程获得锁");
// sleep(1000); //一个线程运行的时间小于3秒最后运行的线程会获得锁
sleep(10000); //一个线程运行的时间大于3秒最后运行的线程会获得锁
}else{
System.out.println(Thread.currentThread().getName()+"线程没有获得锁");
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if(lock.isHeldByCurrentThread()){
//当前线程是否持有锁
lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
ReentrantLock lock = new ReentrantLock();
LockThread lockThread1 = new LockThread(lock);
LockThread lockThread2 = new LockThread(lock);
lockThread1.start();
lockThread2.start();
}
}
Condition class (waiting for notification)
Lock lock=new ReentrantLock(); Condition condition=lock.newCondition(); Obtain a Condition instance. condition.await();//Thread waiting condition.signal();//Wake up thread similar to Object's wait/notify method
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ConditionTest extends Thread{
static Lock lock=new ReentrantLock();
static Condition condition=lock.newCondition();
@Override
public void run() {
try {
lock.lock();
System.out.println("开始等待");
condition.await();//线程等待
System.out.println("等待结束");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public static void main(String[] args) {
ConditionTest conditionTest = new ConditionTest();
conditionTest.start();
try {
Thread.sleep(3000);
lock.lock();
condition.signal(); //唤醒线程
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
getHoldCount
lock.getHoldCount()The number of times lock() is called by the current thread
getQueueLength
lock.getQueueLength() the number of threads in the waiting queue
getWaitQueueLength
lock.getWaitQueueLength(condition) The estimated number of threads in the waiting queue related to the condition
hasQueuedThread
lock.hasQueuedThread(thread) specifies whether the thread is waiting to acquire the lock
hasQueuedThreads
lock.hasQueuedThreads() Is there a thread waiting for the lock
hasWaiters
lock.hasWaiters(condition) Whether there are threads waiting for the specified condition
isFair
lock.isFair() determines whether it is a fair lock
isLocked
lock.isLocked() Whether the current lock is held by the thread
ReentrantReadWriteLock read-write lock
There is a shared lock between read locks (all read threads execute asynchronously), and the read lock and write lock are exclusive.
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class LockTest {
static class ReadWriteLockTest {
ReadWriteLock lock=new ReentrantReadWriteLock();
public void read(){
try {
lock.readLock().lock(); //获得读锁
System.out.println(Thread.currentThread().getName()+"获取读锁:"+System.currentTimeMillis());
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName()+"读锁完成:"+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.readLock().unlock();
}
}
public void write(){
try {
lock.writeLock().lock(); //获得读锁
System.out.println(Thread.currentThread().getName()+"获取写锁:"+System.currentTimeMillis());
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName()+"写锁完成:"+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.writeLock().unlock();
}
}
}
public static void main(String[] args) {
ReadWriteLockTest readWriteLockTest= new ReadWriteLockTest();
for (int i=0;i<5;i++){
new Thread(new Runnable() {
@Override
public void run() {
readWriteLockTest.read();
}
}).start();
}
for (int i=0;i<5;i++){
new Thread(new Runnable() {
@Override
public void run() {
readWriteLockTest.write();
}
}).start();
}
}
}