作者热门文章
- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
ReentrantLock分类FairSync公平锁 - 构造函数true - 线程轮流获得锁NonfairSync非公平锁 - 默认,构造函数false - 跟Synchronized一样是非公平
ReentrantLock
AbstractQueuedSynchronizer
NonfairSync
Sync
Sync
区别1. lockInterruptibly:等待获取锁的线程1,在此过程中,如果被其他线程nterrupt,则该线程1立刻直接不等待获取锁,丢弃锁,直接抛出异常2. lock:等待获取锁的线程1,在此过程中,如果被其他线程interrupt,则延迟到获取锁才interrupt
package top.linruchang.springdemo;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.locks.ReentrantLock;
/**
* 作用:
*
* @author LinRuChang
* @version 1.0
* @date 2021/03/14
* @since 1.8
**/
@Slf4j
public class OtherTest3 {
public static void main(String[] args) throws InterruptedException {
System.out.println("\n=========LockInterruptibly===========\n");
testLockInterruptibly();
Thread.sleep(15000);
System.out.println("\n==========Lock==========\n");
testLock();
}
public static void testLockInterruptibly() throws InterruptedException {
TestLock testLock = new TestLock();
Thread thread1 = new Thread(() -> {
testLock.testLockInterruptibly();
});
thread1.start();
Thread.sleep(500);
Thread thread2 = new Thread(() -> {
testLock.testLockInterruptibly();
});
thread2.start();
Thread.sleep(500);
log.info("中断第二个线程");
thread2.interrupt();
}
public static void testLock() throws InterruptedException {
TestLock testLock = new TestLock();
Thread thread1 = new Thread(() -> {
testLock.testLock();
});
thread1.start();
Thread.sleep(500);
Thread thread2 = new Thread(() -> {
testLock.testLock();
});
thread2.start();
Thread.sleep(500);
log.info("中断第二个线程");
thread2.interrupt();
}
}
@Slf4j
class TestLock {
ReentrantLock reentrantLock = new ReentrantLock();
public void testLockInterruptibly() {
String threadName = Thread.currentThread().getName();
log.info(threadName + "启动");
try {
//获取到锁,但可以调用interceprt使得锁失效
reentrantLock.lockInterruptibly();
log.info(threadName + ":休眠10s");
Thread.sleep(10000);
log.info(threadName + ":休眠起来啦");
} catch (Exception e) {
log.error(threadName + ":发生异常" + e);
} finally {
reentrantLock.unlock();
log.info(threadName + "结束,并释放锁");
}
}
public void testLock() {
String threadName = Thread.currentThread().getName();
log.info(threadName + "启动");
try {
//获取到锁,但可以调用interceprt使得锁失效
reentrantLock.lock();
log.info(threadName + ":第一个休眠10s");
try {
Thread.sleep(10000);
}catch (Exception e) {
log.error(threadName + ":发生异常【第一个休眠】" + e);
}
log.info(threadName + ":第一个休眠起来啦");
log.info(threadName + ":第二个休眠10s");
try {
Thread.sleep(10000);
}catch (Exception e) {
log.error(threadName + ":发生异常【第二个休眠】" + e);
}
log.info(threadName + ":第二个休眠起来啦");
} catch (Exception e) {
log.error(threadName + ":发生异常" + e);
} finally {
reentrantLock.unlock();
log.info(threadName + "结束,并释放锁");
}
}
}
Java-JUC ⓪基础 ❶进程&线程 进程:指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间。 线程:比进程更小的执行单位,一个进程可
ReentrantLock分类FairSync公平锁 - 构造函数true - 线程轮流获得锁NonfairSync非公平锁 - 默认,构造函数false - 跟Synchronized一样是非公平
我正在学习JUC,我想计算程序运行五个线程的总时间,但是打印“1 2 3”后就阻塞了。请告诉我是什么原因? 另外,如果我不调用函数“isPrime(int)”,程序会正常执行。 public clas
我是一名优秀的程序员,十分优秀!