一、Lock是java并发同步代码块的工具之一。我们可以通过监控Lock知道哪个线程获取到了锁,哪个线程释放了锁,那些线程正在等待锁。
二、监控Lock
package com.np.ota.test.synctest.lock; import java.util.Collection; import java.util.concurrent.locks.ReentrantLock; /** * 自定义Lock * @author luke */ public class MyLock extends ReentrantLock{ private static final long serialVersionUID = 1L; public MyLock(){ super(true); } public String getOwnerName(){ if(this.getOwner() == null){ return "none"; } return this.getOwner().getName(); } //Returns a collection containing threads that may be waiting to acquire this lock //Because the actual set of threads may change dynamically while constructing this result //获取等待该锁的所有线程集合,但结果不是绝对准确的 public Collection<Thread> getThreads(){ return this.getQueuedThreads(); } }
package com.np.ota.test.synctest.lock; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; public class Task implements Runnable{ private Lock lock; public Task(Lock lock) { this.lock = lock; } @Override public void run() { for(int i = 0; i < 5; i++){ lock.lock(); System.out.println(Thread.currentThread().getName()+" get the lock");//获得锁的线程 try { TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }finally{ System.out.println(Thread.currentThread().getName()+" free the lock");//线程释放锁 lock.unlock(); } } } }
package com.np.ota.test.synctest.lock; import java.util.Collection; import java.util.concurrent.TimeUnit; /** * 监控Lock * @author luke */ public class Main { public static void main(String[] args){ //创建自定义锁,方便监控 MyLock lock = new MyLock(); //创建5个线程,并执行 Thread[] threads = new Thread[5]; for(int i = 0; i < 5; i++){ Task task = new Task(lock); threads[i] = new Thread(task); threads[i].start(); } //监控Lock for(int i = 0; i < 15; i++){ System.out.println("***************************"); System.out.println("Lock Owner : "+lock.getOwnerName());//锁的拥有者 System.out.println("Lock queued Threads: "+lock.hasQueuedThreads());//是否有线程正在等待获取该锁 if(lock.hasQueuedThreads()){ System.out.println("waitting Lock queue length: "+lock.getQueueLength());//该瞬间等待该锁的线程数 Collection<Thread> lockthreads = lock.getThreads();//遍历该瞬间等待该锁的线程 for(Thread lockthread : lockthreads){ System.out.println("waitting lock thread:"+lockthread.getName()); } } System.out.println("lock fairness:"+lock.isFair()); System.out.println("lock locked:"+lock.isLocked()); System.out.println("***************************"); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } }
结果:
Thread-0 get the lock
***************************
Lock Owner : Thread-0
Lock queued Threads: true
waitting Lock queue length: 1
waitting lock thread:Thread-3
lock fairness:true
lock locked:true
***************************
Thread-0 free the lock
Thread-3 get the lock
Thread-3 free the lock
***************************
Lock Owner : Thread-3
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-0
waitting lock thread:Thread-4
waitting lock thread:Thread-2
waitting lock thread:Thread-1
lock fairness:true
lock locked:true
***************************
Thread-1 get the lock
Thread-1 free the lock
Thread-2 get the lock
***************************
Thread-2 free the lock
Lock Owner : Thread-2
Thread-4 get the lock
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-2
waitting lock thread:Thread-1
waitting lock thread:Thread-3
waitting lock thread:Thread-0
lock fairness:true
lock locked:true
***************************
Thread-4 free the lock
Thread-0 get the lock
Thread-0 free the lock
Thread-3 get the lock
***************************
Lock Owner : Thread-3
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-0
waitting lock thread:Thread-4
waitting lock thread:Thread-2
waitting lock thread:Thread-1
lock fairness:true
lock locked:true
***************************
Thread-3 free the lock
Thread-1 get the lock
Thread-1 free the lock
Thread-2 get the lock
***************************
Lock Owner : Thread-2
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-1
waitting lock thread:Thread-3
waitting lock thread:Thread-0
waitting lock thread:Thread-4
lock fairness:true
lock locked:true
***************************
Thread-2 free the lock
Thread-4 get the lock
Thread-4 free the lock
Thread-0 get the lock
***************************
Lock Owner : Thread-0
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-4
waitting lock thread:Thread-2
waitting lock thread:Thread-1
waitting lock thread:Thread-3
lock fairness:true
lock locked:true
***************************
Thread-0 free the lock
Thread-3 get the lock
Thread-3 free the lock
Thread-1 get the lock
***************************
Lock Owner : Thread-1
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-3
waitting lock thread:Thread-0
waitting lock thread:Thread-4
waitting lock thread:Thread-2
lock fairness:true
lock locked:true
***************************
Thread-1 free the lock
Thread-2 get the lock
Thread-2 free the lock
Thread-4 get the lock
***************************
Lock Owner : Thread-4
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-2
waitting lock thread:Thread-1
waitting lock thread:Thread-3
waitting lock thread:Thread-0
lock fairness:true
lock locked:true
***************************
Thread-4 free the lock
Thread-0 get the lock
Thread-0 free the lock
Thread-3 get the lock
***************************
Lock Owner : Thread-3
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-0
waitting lock thread:Thread-4
waitting lock thread:Thread-2
waitting lock thread:Thread-1
lock fairness:true
lock locked:true
***************************
Thread-3 free the lock
Thread-1 get the lock
Thread-1 free the lock
Thread-2 get the lock
***************************
Lock Owner : Thread-2
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-1
waitting lock thread:Thread-3
waitting lock thread:Thread-0
waitting lock thread:Thread-4
lock fairness:true
lock locked:true
***************************
Thread-2 free the lock
Thread-4 get the lock
Thread-4 free the lock
Thread-0 get the lock
***************************
Lock Owner : Thread-0
Lock queued Threads: true
waitting Lock queue length: 4
waitting lock thread:Thread-4
waitting lock thread:Thread-2
waitting lock thread:Thread-1
waitting lock thread:Thread-3
lock fairness:true
lock locked:true
***************************
Thread-0 free the lock
Thread-3 get the lock
Thread-3 free the lock
Thread-1 get the lock
***************************
Lock Owner : Thread-1
Lock queued Threads: true
waitting Lock queue length: 2
waitting lock thread:Thread-4
waitting lock thread:Thread-2
lock fairness:true
lock locked:true
***************************
Thread-1 free the lock
Thread-2 get the lock
Thread-2 free the lock
Thread-4 get the lock
***************************
Lock Owner : Thread-4
Lock queued Threads: false
lock fairness:true
lock locked:true
***************************
Thread-4 free the lock
***************************
Lock Owner : none
Lock queued Threads: false
lock fairness:true
lock locked:false
***************************
***************************
Lock Owner : none
Lock queued Threads: false
lock fairness:true
lock locked:false
***************************