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