ReentrantLock 类的 lock() 方法持有锁,如果它没有被其他线程持有并立即返回,并将锁持有计数设置为 1。
用法
public void lock()
参数
没有传递参数。
返回
没有返回类型。
抛出
不会抛出异常。
例子1
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 WorkerClassLL implements Runnable
{
String nm;
ReentrantLock relc;
public WorkerClassLL(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)
{
relc.lock();
Thread t = new Thread();
System.out.println("Thread Created.....");
if(i==3) {
t.setName("Maint Thread2");
System.out.println("Thread Created.....");
}
}
if(i==4)
relc.unlock();
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 ReentrantLocklockExample1
{
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 WorkerClassLL(rel, "Job1");
pool.execute(wrk1);
pool.shutdown();
}
}
输出:
after sleep(1500) Is locked - true 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 WorkerClassC2 implements Runnable
{
String nm;
ReentrantLock relc;
public WorkerClassC2(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 ReentrantLocklockExample2
{
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 WorkerClassC2(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;@6bd19be0 after sleep(1500) Is held by Current Thread - true task name - Job1 work done
相关用法
- Java ReentrantLock lockInterruptibly()用法及代码示例
- Java ReentrantLock toString()用法及代码示例
- Java ReentrantLock tryLock()用法及代码示例
- Java ReentrantLock isHeldByCurrentThread()用法及代码示例
- Java ReentrantLock isLocked()用法及代码示例
- Java ReentrantLock getHoldCount()用法及代码示例
- Java ReentrantLock isFair()用法及代码示例
- Java ReentrantLock hasQueuedThread()用法及代码示例
- Java ReentrantLock getOwner()用法及代码示例
- Java ReentrantLock hasQueuedThreads()用法及代码示例
- 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 lock() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。