ReentrantLock 类的 toString() 方法返回一个字符串来标识这个锁和它的锁状态。
用法
public String toString()
参数
没有传递参数。
返回
标识此锁及其锁状态的字符串
抛出
不会抛出异常。
例子1
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
class ToStringClass implements Runnable
{
String nm;
ReentrantLock relc;
public ToStringClass(ReentrantLock rl, String n)
{
relc = rl;
nm = n;
}
public void run()
{
boolean dn = false;
while (!dn)
{
try {
int c=0;
if(relc.tryLock())
{
int i;
Thread t1=new Thread();
for( i =5 ; i <= 10 ; i++)
System.out.println(t1.getName());
t1.checkAccess();
t1.countStackFrames();
c=c+1;
if(i>7)
{
System.out.println(t1.getName());
break;
}
}
}
finally
{
//Outer lock release
System.out.println("task name - " + nm +
" releasing lock(outer lock)");
relc.unlock();
System.out.println("Lock Hold Count - " +
relc.getHoldCount());
}
}
}
}
public class ReentrantLoctoStringExample1
{
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 ToStringClass(rel, "Job1");
System.out.println(" " +wrk1.toString());
pool.execute(wrk1);
System.out.println(" " +pool.toString());
pool.shutdown();
}
}
输出:
[email protected] Thread-0 Thread-0 Thread-0 Thread-0 Thread-0 Thread-0 [email protected][Running, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0] Thread-0 task name - Job1 releasing lock(outer lock) Lock Hold Count - 0
例子2
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLoctoStringExample2 {
public static void main(String[] args) {
ReentrantLock lock=new ReentrantLock();
System.out.println(" " +lock.toString());
ToStringClass2 myRnbl=new ToStringClass2(lock);
System.out.println(" " +myRnbl.toString());
new Thread(myRnbl,"Thread-1").start();
}
}
class ToStringClass2 implements Runnable{
ReentrantLock lock;
public ToStringClass2(ReentrantLock lock) {
this.lock=lock;
}
public void run(){
System.out.println(Thread.currentThread().getName()
+" is Waiting to get the lock");
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;
}
assert !lock.isHeldByCurrentThread();
lock.lock();
System.out.println(" after sleep(1500) Is held by Current Thread - " + lock.isHeldByCurrentThread());
}
finally {
lock.unlock();
}
}
}
输出:
[email protected][Unlocked] [email protected] Thread-1 is Waiting to get the lock 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;@12a128ac after sleep(1500) Is held by Current Thread - true
相关用法
- Java ReentrantLock tryLock()用法及代码示例
- Java ReentrantLock lock()用法及代码示例
- Java ReentrantLock isHeldByCurrentThread()用法及代码示例
- Java ReentrantLock isLocked()用法及代码示例
- Java ReentrantLock getHoldCount()用法及代码示例
- Java ReentrantLock isFair()用法及代码示例
- 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 toString() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。