亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
乌苏市| 开原市| 肇州县| 天长市| 卓资县| 云林县| 永安市| 迁西县| 崇阳县| 房山区| 化州市| 塔河县| 华安县| 徐水县| 卢氏县| 沙洋县| 桂平市| 交城县| 定襄县| 青阳县| 广饶县| 盐津县| 洛隆县| 八宿县| 时尚| 五原县| 微博| 贵阳市| 黑水县| 佛坪县| 大足县| 会理县| 松原市| 搜索| 丹巴县| 沂源县| 海晏县| 涟源市| 台南县| 淳安县| 博罗县|