如果获取此锁的公平性为真,则 ReentrantLock 类的 isFair() 方法返回真。
用法
public final boolean isFair()
参数
线程 - 线程
返回
如果此锁的公平性设置为 true,则为 true
抛出
不抛出异常
例子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 Workerclass2 implements Runnable
{
String nm;
ReentrantLock relc;
public Workerclass2(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
{
Date d = new Date();
SimpleDateFormat frmt = new SimpleDateFormat("hh:mm:ss");
System.out.println("task name is- "+ nm
+ " outer lock at "
+ frmt.format(d)
+ " outer work");
Thread.sleep(1500);
// Inner Lock
relc.lock();
try
{
d = new Date();
frmt = new SimpleDateFormat("hh:mm:ss");
System.out.println("task name is- "+ nm
+ " inner lock at "
+ frmt.format(d)
+ " inner work");
System.out.println("Lock Hold Count is - "+ relc.getHoldCount());
Thread.sleep(1500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Lock is Fair - " + relc.isFair());
System.out.println("task name - " + nm + " work done");
dn = true;
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
public class ReentrantLockisFairExample1
{
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 Workerclass2(rel, "Job1");
pool.execute(wrk1);
pool.shutdown();
}
}
输出:
task name is- Job1 outer lock at 10:11:17 outer work task name is- Job1 inner lock at 10:11:18 inner work Lock Hold Count is - 2 Lock is Fair - false task name - Job1 work done
例子2
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 WorkerClass1 implements Runnable
{
String nm;
ReentrantLock relc;
public WorkerClass1(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
{
Date dt = new Date();
SimpleDateFormat frmt = new SimpleDateFormat("hh:mm:ss");
System.out.println("task name - "+ nm
+ " outer lock at "
+ frmt.format(dt)
+ " outer work");
Thread.sleep(1500);
// Inner Lock
relc.lock();
try
{
dt = new Date();
frmt = new SimpleDateFormat("hh:mm:ss");
System.out.println("task name - "+ nm
+ " inner lock acquired at "
+ frmt.format(dt)
+ " Doing inner work");
System.out.println("Lock Hold Count - "+ relc.getHoldCount());
Thread.sleep(1500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
//Inner lock release
System.out.println("task name - " + nm+
" releasing lock (inner lock)");
relc.unlock();
}
System.out.println("Lock Hold Count - " + relc.getHoldCount());
System.out.println("task name - " + nm + " work done");
dn = true;
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
//Outer lock release
System.out.println("task name - " + nm +
" releasing lock(outer lock)");
relc.unlock();
System.out.println("Lock is Fair - " +
relc.isFair());
}
}
else
{
System.out.println("task name - " + nm +
" waiting for lock");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
public class ReentrantLockisFairExample2
{
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 WorkerClass1(rel, "Job1");
Runnable wrk2 = new WorkerClass1(rel, "Job2");
pool.execute(wrk1);
pool.execute(wrk2);
pool.shutdown();
}
}
输出:
task name - Job2 waiting for lock task name - Job1 outer lock at 10:12:48 outer work task name - Job2 waiting for lock task name - Job1 inner lock acquired at 10:12:50 Doing inner work Lock Hold Count - 2 task name - Job2 waiting for lock task name - Job2 waiting for lock task name - Job1 releasing lock (inner lock) Lock Hold Count - 1 task name - Job1 work done task name - Job1 releasing lock(outer lock) Lock is Fair - false task name - Job2 outer lock at 10:12:52 outer work task name - Job2 inner lock acquired at 10:12:54 Doing inner work Lock Hold Count - 2 task name - Job2 releasing lock (inner lock) Lock Hold Count - 1 task name - Job2 work done task name - Job2 releasing lock(outer lock) Lock is Fair - false
相关用法
- Java ReentrantLock isHeldByCurrentThread()用法及代码示例
- Java ReentrantLock isLocked()用法及代码示例
- 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 isFair() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。