ReentrantLock ReentrantLock功能 ReentrantLock和synchronized一樣是可重入的 可重入即當線程擁有了鎖時,當該線程再次請求鎖資源的時候,線程是可以再次成功獲得的。 static ReentrantLock lock = new ReentrantLoc ...
ReentrantLock
ReentrantLock功能
-
ReentrantLock和synchronized一樣是可重入的
-
可重入即當線程擁有了鎖時,當該線程再次請求鎖資源的時候,線程是可以再次成功獲得的。
-
static ReentrantLock lock = new ReentrantLock(); public static void main(String[] args) { new Thread(() ->{ lock.lock(); try{ log.debug("獲得了鎖,再次請求鎖"); lock.lock(); try{ log.debug("再次獲得了鎖"); }finally { log.debug("釋放了鎖"); lock.unlock(); } }finally { log.debug("釋放了鎖"); lock.unlock(); } },"t1").start(); }
-
-
ReentrantLock是可打斷的
-
即請求鎖資源的時候,當請求不到鎖資源的時候,可以被interrupt方法打斷。
-
static ReentrantLock lock = new ReentrantLock(); public static void main(String[] args) { Thread t1 = new Thread(() -> { try { // 如果當lock請求鎖資源的同時被其他其他線程調用了interrupt方法 // 就會進入異常狀態 lock.lockInterruptibly(); } catch (InterruptedException e) { e.printStackTrace(); log.debug("請求鎖被打斷"); return; // 當請求鎖被打斷後沒有獲得鎖,不應該在進入下麵的語句 } try { log.debug("獲得到了鎖資源"); } finally { lock.unlock(); } }, "t1"); lock.lock(); t1.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } t1.interrupt(); }
-
-
ReentrantLock可以鎖超時
-
synchronized請求鎖會一直地等待鎖資源,而ReentrantLock請求鎖不會無限制的進行下去,使用tryLock()方法,可以在一次請求鎖資源或請求一段時間的鎖資源後結束請求鎖。
-
static ReentrantLock lock = new ReentrantLock(); public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { log.debug("開始運行了"); // 該情況下只會請求一次鎖資源,請求到鎖資源返回true,否則返回false // 加上時間限制的trylock是一樣的。 try { if (lock.tryLock(2, TimeUnit.SECONDS)) { try { log.debug("請求到了鎖資源"); } finally { lock.unlock(); } } else { log.debug("沒請求到鎖資源"); } } catch (InterruptedException e) { e.printStackTrace(); } }, "t1"); lock.lock(); log.debug("上鎖了"); t1.start(); Thread.sleep(1000); log.debug("解鎖了"); lock.unlock(); }
-
-
當一些線程一直無法獲得鎖資源時,使用公平鎖就可以時獲得鎖變成先進先獲得。
-
static ReentrantLock lock = new ReentrantLock(true);
-
-
條件變數,即ReentrantLock支持多條件的進入不同的WaitSet進行等待,synchronized就只有一個WaitSet隊列。
-
當出現需要不同條件進入等待就可以使用該條件變數。
-
static ReentrantLock lock = new ReentrantLock(); static Condition waitCondition1Set = lock.newCondition(); // 然後調用condition的await方法即可進入等待狀態。
-
設計模式---順序執行
保證線程執行的一致性,因為這裡是同一個鎖對象所以不能用join
wait-notify
@Slf4j
public class Test5 {
static boolean already = false;
public static void main(String[] args) {
final Object o = new Object();
Thread t1 = new Thread(() -> {
synchronized (o){
while (!already){
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.debug("1");
}
}, "t1");
Thread t2 = new Thread(() -> {
synchronized (o){
log.debug("2");
already = true;
o.notify();
}
}, "t2");
t1.start();
t2.start();
}
}
park-unpark
@Slf4j
public class Test6 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
LockSupport.park();
log.debug("1");
}, "t1");
Thread t2 = new Thread(() -> {
LockSupport.park();
log.debug("2");
LockSupport.unpark(t1);
}, "t2");
t1.start();
t2.start();
Thread.sleep(2000);
LockSupport.unpark(t2);
}
}
設計模式---交替輸出
三個線程交替輸出
wait-notify
public class Test7 {
public static void main(String[] args) {
PrintObject object = new PrintObject(5,2);
Thread t1 = new Thread(() -> {
object.print(2,1);
}, "t1");
Thread t2 = new Thread(() -> {
object.print(3,2);
}, "t2");
Thread t3 = new Thread(() -> {
object.print(1,3);
}, "t3");
t1.start();
t2.start();
t3.start();
}
}
class PrintObject{
private int loopNum;
private int flag;
public PrintObject(int loopNum,int flag) {
this.loopNum = loopNum;
this.flag = flag;
}
public synchronized void print(int next,int now){
for(int i = 0;i < loopNum;i++){
while (flag != now){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(now);
flag = next;
notifyAll();
}
}
}
await-signal
class PrintObject{
private ReentrantLock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private int loopNum;
private int flag;
public PrintObject(int loopNum,int flag) {
this.loopNum = loopNum;
this.flag = flag;
}
public void print(int next,int now){
lock.lock();
try{
for(int i = 0;i < loopNum;i++){
while (flag != now){
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(flag);
flag = next;
condition.signalAll();
}
}finally {
lock.unlock();
}
}
}
park-unpark
@Slf4j
public class Test8 {
static Thread t1;
static Thread t2;
static Thread t3;
public static void main(String[] args) throws InterruptedException {
t1 = new Thread(() -> {
LockSupport.park();
log.debug("1");
LockSupport.unpark(t2);
}, "t1");
t2 = new Thread(() -> {
LockSupport.park();
log.debug("2");
LockSupport.unpark(t3);
}, "t2");
t3 = new Thread(() -> {
LockSupport.park();
log.debug("3");
LockSupport.unpark(t1);
}, "t3");
t1.start();
t2.start();
t3.start();
Thread.sleep(1000);
LockSupport.unpark(t1);
}
}