91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

Java LockInfo類使用實例

小云
96
2023-09-28 08:32:43
欄目: 編程語言

下面是一個使用Java LockInfo類的示例:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockInfoExample {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
// 獲取鎖的信息
LockInfo lockInfo = lock.getLockInfo();
System.out.println("Lock owner: " + lockInfo.getOwnerName());
System.out.println("Lock is held by current thread: " + lockInfo.isLocked());
System.out.println("Lock is fair: " + lockInfo.isFair());
// 鎖定
lock.lock();
// 獲取鎖的信息
lockInfo = lock.getLockInfo();
System.out.println("Lock owner: " + lockInfo.getOwnerName());
System.out.println("Lock is held by current thread: " + lockInfo.isLocked());
System.out.println("Lock is fair: " + lockInfo.isFair());
// 解鎖
lock.unlock();
// 獲取鎖的信息
lockInfo = lock.getLockInfo();
System.out.println("Lock owner: " + lockInfo.getOwnerName());
System.out.println("Lock is held by current thread: " + lockInfo.isLocked());
System.out.println("Lock is fair: " + lockInfo.isFair());
}
}

輸出結果:

Lock owner: null
Lock is held by current thread: false
Lock is fair: false
Lock owner: main
Lock is held by current thread: true
Lock is fair: false
Lock owner: null
Lock is held by current thread: false
Lock is fair: false

這個示例創建了一個ReentrantLock對象,并使用LockInfo類獲取了鎖的信息。在獲取鎖之前,鎖的擁有者是null,鎖沒有被任何線程持有。在獲取鎖之后,鎖的擁有者是主線程,鎖被當前線程持有。在釋放鎖之后,鎖的擁有者再次變為null,鎖沒有被任何線程持有。

0
自治县| 寿阳县| 龙岩市| 中卫市| 任丘市| 凉山| 梅州市| 江城| 蓝山县| 安西县| 余干县| 泽州县| 肥西县| 株洲县| 崇礼县| 平潭县| 宜阳县| 旺苍县| 阆中市| 西安市| 江安县| 鲁甸县| 松滋市| 仁寿县| 铜鼓县| 朝阳县| 中西区| 新兴县| 平潭县| 汽车| 常德市| 于都县| 女性| 开封县| 平果县| 铁力市| 揭阳市| 治多县| 彭泽县| 错那县| 社会|