import java.util.concurrent.locks.ReentrantLock; public class ReentrantLockDemo01 implements Runnable { private static ReentrantLock reentrantLock = new ReentrantLock(true); @Override public void run() { while (true){ reentrantLock.lock(); try{ System.out.println(Thread.currentThread().getName()+" get lock"); Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); }finally { reentrantLock.unlock(); } } } public static void main(String[] args) { ReentrantLockDemo01 reentrantLockDemo01 = new ReentrantLockDemo01(); Thread thread1 = new Thread(reentrantLockDemo01); Thread thread2 = new Thread(reentrantLockDemo01); thread1.start(); thread2.start(); } }
ReentrantLock公平锁demo
最新推荐文章于 2022-07-29 17:09:07 发布