Lock锁
源码
| public interface Lock { |
| void lock(); |
| void lockInterruptibly() throws InterruptedException; |
| boolean tryLock(); |
| boolean tryLock(long time, TimeUnit unit) throws InterruptedException; |
| void unlock(); |
| Condition newCondition(); |
| } |
| ReentrantLock() |
| ReentrantLock(boolean fair) |
| |
| int getHoldCount() |
| protected Thread getOwner() |
| protected Collection<Thread> getQueuedThreads() |
| int getQueueLength() |
| protected Collection<Thread> getWaitingThreads(Condition condition) |
| int getWaitQueueLength(Condition condition) |
| boolean hasQueuedThread(Thread thread) |
| boolean hasQueuedThreads() |
| boolean hasWaiters(Condition condition) |
| boolean isFair() |
| boolean isHeldByCurrentThread() |
| boolean isLocked() |
| void lock() |
| void lockInterruptibly() |
| Condition newCondition() |
| boolean tryLock() |
| boolean tryLock(long timeout, TimeUnit unit) |
| void unlock() |
| void await() |
| boolean await(long time, TimeUnit unit) |
| long awaitNanos(long nanosTimeout) |
| void awaitUninterruptibly() |
| boolean awaitUntil(Date deadline) |
| void signal() |
| void signalAll() |
获取锁
| |
| Lock lock=new ReentrantLock(); |
| lock.lock(); |
| try{ |
| |
| }catch(Exception ex){ |
| |
| }finally{ |
| lock.unlock(); |
| } |
| Lock lock=new ReentrantLock(); |
| if(lock.tryLock()) { |
| try{ |
| |
| }catch(Exception ex){ |
| |
| }finally{ |
| lock.unlock(); |
| } |
| }else { |
| |
| } |
| |
| public void method() throws InterruptedException { |
| Lock lock=new ReentrantLock(); |
| lock.lockInterruptibly(); |
| try { |
| |
| } |
| finally { |
| lock.unlock(); |
| } |
| } |
| public ReentrantLock() { |
| sync = new NonfairSync(); |
| } |
| public ReentrantLock(boolean fair) { |
| sync = fair ? new FairSync() : new NonfairSync(); |
| } |
与synchronized区别
- Synchronized 内置的java关键字,lock是一个java类
- Synchronized **无法判断获取锁的状态 **,lock **可以判断是否获取了锁 **
- Synchronized 会 **自动释放锁 **,lock **必须手动释放锁,否则死锁 **
- Synchronized 线程1(获得锁,阻塞),线程2(等待,继续等待);lock不一定会等待下去
- Synchronized **可重入锁,不可以中断,非公平 **;lock,可重入锁,可以判断锁,非公平(可以自己设置)
- Synchronized 适合锁少量代码同步问题,lock锁大量同步代码
lock版的
| |
| import java.util.concurrent.locks.Condition; |
| import java.util.concurrent.locks.Lock; |
| import java.util.concurrent.locks.ReentrantLock; |
| |
| class B { |
| |
| public static void main(String[] args) { |
| Data2 data2 = new Data2(); |
| |
| new Thread(() -> { |
| for (int i = 0; i < 10; i++) { |
| try { |
| data2.increment(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| }, "A").start(); |
| new Thread(() -> { |
| for (int i = 0; i < 10; i++) { |
| try { |
| data2.decrement(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| }, "B").start(); |
| new Thread(() -> { |
| for (int i = 0; i < 10; i++) { |
| try { |
| data2.increment(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| }, "C").start(); |
| new Thread(() -> { |
| for (int i = 0; i < 10; i++) { |
| try { |
| data2.decrement(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| }, "D").start(); |
| } |
| } |
| |
| class Data2 { |
| private int num = 0; |
| private Lock lock = new ReentrantLock(); |
| private Condition condition = lock.newCondition(); |
| |
| |
| public void increment() throws InterruptedException { |
| lock.lock(); |
| try { |
| while (num != 0) { |
| condition.await(); |
| } |
| num++; |
| System.out.println(Thread.currentThread().getName() + "->" + num); |
| condition.signalAll(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| |
| |
| |
| public void decrement() throws InterruptedException { |
| lock.lock(); |
| try { |
| while (num == 0) { |
| condition.await(); |
| } |
| num--; |
| System.out.println(Thread.currentThread().getName() + "->" + num); |
| condition.signalAll(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| } |
| |
| import java.util.concurrent.locks.Condition; |
| import java.util.concurrent.locks.Lock; |
| import java.util.concurrent.locks.ReentrantLock; |
| |
| class C { |
| public static void main(String[] args) { |
| Data3 data3 = new Data3(); |
| |
| new Thread(() -> { |
| for (int i = 0; i < 10; i++) { |
| data3.printA(); |
| } |
| }, "A").start(); |
| new Thread(() -> { |
| for (int i = 0; i < 10; i++) { |
| data3.printB(); |
| } |
| }, "B").start(); |
| new Thread(() -> { |
| for (int i = 0; i < 10; i++) { |
| data3.printC(); |
| } |
| }, "C").start(); |
| } |
| } |
| |
| |
| class Data3 { |
| private int num = 1; |
| private Lock lock = new ReentrantLock(); |
| private Condition condition1 = lock.newCondition(); |
| private Condition condition2 = lock.newCondition(); |
| private Condition condition3 = lock.newCondition(); |
| |
| public void printA() { |
| lock.lock(); |
| try { |
| while (num != 1) { |
| condition1.await(); |
| } |
| num = 2; |
| System.out.println(Thread.currentThread().getName() + "AAA"); |
| condition2.signalAll(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| |
| public void printB() { |
| lock.lock(); |
| try { |
| while (num != 2) { |
| condition2.await(); |
| } |
| num = 3; |
| System.out.println(Thread.currentThread().getName() + "BBB"); |
| condition3.signalAll(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| |
| public void printC() { |
| lock.lock(); |
| try { |
| while (num != 3) { |
| condition3.await(); |
| } |
| num = 1; |
| System.out.println(Thread.currentThread().getName() + "CCC"); |
| condition1.signalAll(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/15348575.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步