ReentrantLock 類的 isLocked() 方法檢查是否有任何線程占用此鎖。創建此方法是為了監視係統狀態,而不是用於同步控製。
用法
public boolean isLocked()
參數
沒有傳遞參數。
返回
如果任何線程持有此鎖,則為 true,否則為 false
拋出
不拋出異常
例子1
//import statement
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
class WorkerClass23 implements Runnable
{
String nm;
ReentrantLock relc;
public WorkerClass23(ReentrantLock rl, String n)
{
relc = rl;
nm = n;
}
public void run()
{
boolean dn = false;
while (!dn)
{
//Outer Lock
boolean ans = relc.tryLock();
// True if lock is free
if(ans)
{
try
{
int i=0;
for(i=0;i<=6;i++) {
if(i>=2)
{
Thread t = new Thread();
System.out.println("Thread Created.....");
if(i==3) {
t.setName("Maint Thread2");
System.out.println("Thread Created.....");
}
}
if(i==4)
break;
}
System.out.println(" after sleep(1500) Is locked - " + relc.isLocked());
System.out.println("task name - " + nm + " work done");
dn = true;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
public class ReentrantLockisLockedExample1
{
static final int MAX_Time = 2;
public static void main(String[] args)
{
ReentrantLock rel = new ReentrantLock();
ExecutorService pool = Executors.newFixedThreadPool(MAX_Time);
Runnable wrk1 = new WorkerClass23(rel, "Job1");
pool.execute(wrk1);
pool.shutdown();
}
}
輸出:
Thread Created..... Thread Created..... Thread Created..... Thread Created..... after sleep(1500) Is locked - true task name - Job1 work done
例子2
//import statements
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
class WorkerClass24 implements Runnable
{
String nm;
ReentrantLock relc;
public WorkerClass24(ReentrantLock rl, String n)
{
relc = rl;
nm = n;
}
public void run()
{
boolean dn = false;
while (!dn)
{
//Outer Lock
boolean ans = relc.tryLock();
// True if lock is free
if(ans)
{
try
{
int i=1;
Thread t=new Thread();
while(i<5) {
System.out.println(" "+t.getThreadGroup());
if(i==2)
System.out.println(" "+t.getThreadGroup());
if(i==3)
System.out.println(" "+t.isInterrupted());
if(i==4)
System.out.println(" "+t.getStackTrace());
i=i+1;
}
if(relc.isLocked())
{
}
else {
assert !relc.isHeldByCurrentThread();
relc.lock();
}
System.out.println(" after sleep(1500) Is held by Current Thread - " + relc.isHeldByCurrentThread());
System.out.println("task name - " + nm + " work done");
dn = true;
}
finally {
relc.unlock();
}
}
}
}
}
public class ReentrantLockisLockedExample2
{
static final int MAX_Time = 2;
public static void main(String[] args)
{
ReentrantLock rel = new ReentrantLock();
ExecutorService pool = Executors.newFixedThreadPool(MAX_Time);
Runnable wrk1 = new WorkerClass24(rel, "Job1");
pool.execute(wrk1);
pool.shutdown();
}
}
輸出:
java.lang.ThreadGroup[name=main,maxpri=10] java.lang.ThreadGroup[name=main,maxpri=10] java.lang.ThreadGroup[name=main,maxpri=10] java.lang.ThreadGroup[name=main,maxpri=10] false java.lang.ThreadGroup[name=main,maxpri=10] [Ljava.lang.StackTraceElement;@3127c164 after sleep(1500) Is held by Current Thread - true task name - Job1 work done
相關用法
- Java ReentrantLock isHeldByCurrentThread()用法及代碼示例
- Java ReentrantLock isFair()用法及代碼示例
- Java ReentrantLock lock()用法及代碼示例
- Java ReentrantLock toString()用法及代碼示例
- Java ReentrantLock tryLock()用法及代碼示例
- Java ReentrantLock getHoldCount()用法及代碼示例
- Java ReentrantLock hasQueuedThread()用法及代碼示例
- Java ReentrantLock getOwner()用法及代碼示例
- Java ReentrantLock hasQueuedThreads()用法及代碼示例
- Java ReentrantLock lockInterruptibly()用法及代碼示例
- Java Reentrant getQueueLength()用法及代碼示例
- Java Reader read(CharBuffer)用法及代碼示例
- Java Reader markSupported()用法及代碼示例
- Java Reader ready()用法及代碼示例
- Java ResolverStyle valueOf()用法及代碼示例
- Java ResourceBundle containsKey()用法及代碼示例
- Java ResourceBundle getString()用法及代碼示例
- Java ResourceBundle getKeys()用法及代碼示例
- Java ResourceBundle clearCache()用法及代碼示例
- Java Reader close()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ReentrantLock isLocked() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。