Java Explicit Locks
来源:互联网 发布:seo 百度移动搜索收录 编辑:程序博客网 时间:2024/06/10 04:34
Explicit locking mechanism can be used to coordinate access to shared resources in a multi-threaded environment.
The Lock interface, which is declared in the java.util.concurrent.locks package, defines the explicit locking operations.
The ReentrantLock class, in the same package, is the concrete implementation of the Lock interface.
The Lock interface is declared as follows:
public interface Lock { void lock(); Condition newCondition(); void lockInterruptibly() throws InterruptedException; boolean tryLock(); boolean tryLock(long time, TimeUnit unit) throws InterruptedException; void unlock();}
lock() method acquires a lock behaves the same as the use of the synchronized keyword.
We must release the lock by calling the unlock() method of the Lock interface after we are done with the lock.
import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;//from w ww .j a v a 2 s .co mpublic class Main { // Instantiate the lock object private Lock myLock = new ReentrantLock(); public void updateResource() { try { // Acquire the lock myLock.lock(); } finally { // Release the lock myLock.unlock(); } }}
Example
The following code models the philosophers assuming that an object of the ReentrantLock class represents a fork.
import java.util.concurrent.locks.Lock;// w w w . ja v a 2s .co mclass Philosopher { private Lock leftFork; private Lock rightFork; private String name; // Philosopher's name public Philosopher(Lock leftFork, Lock rightFork, String name) { this.leftFork = leftFork; this.rightFork = rightFork; this.name = name; } public void think() { System.out.println(name + " is thinking..."); } public void eat() { if (leftFork.tryLock()) { try { if (rightFork.tryLock()) { try { System.out.println(name + " is eating..."); } finally { rightFork.unlock(); } } } finally { leftFork.unlock(); } } }}
Example 2
A ReentrantReadWriteLock class is an implementation of the ReadWriteLock Interface. Only one thread can hold the write lock of ReentrantReadWriteLock, whereas multiple threads can hold its read lock.
The following code demonstrates the usage of ReentrantReadWriteLock.
import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantReadWriteLock;/*from w w w. j a v a 2 s . co m*/public class Main { private int value; private ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); private Lock rLock = rwLock.readLock(); private Lock wLock = rwLock.writeLock(); public Main(int value) { this.value = value; } public int getValue() { rLock.lock(); try { return this.value; } finally { rLock.unlock(); } } public void setValue(int value) { wLock.lock(); try { this.value = value; } finally { wLock.unlock(); } }}
- Java Explicit Locks
- java.util.concurrent.locks.Locks和synchronized
- java.util.concurrent.locks
- java.util.concurrent.locks简述
- java.util.concurrent.locks Synchronized
- java.util.concurrent.locks.ReentrantLock
- java.util.concurrent.locks.AbstractOwnableSynchronizer
- java.util.concurrent.locks.Lock
- java.util.concurrent.locks.LockSupport
- 软件包 java.util.concurrent.locks
- Java.concurrent.locks(2)-ReentrantLock
- Read / Write Locks in Java
- [1]-1 Locks in Java
- java.util.concurrent.locks包
- java.util.concurrent.locks.AbstractOwnableSynchronizer
- java.util.concurrent.locks.Lock
- java.io.IOException: No locks available
- 使用java.util.concurrent.locks代替synchronized
- ARM寄存器(R13/R14/R15)
- 第一章作业
- 《JavaScript高级程序设计》函数
- 几个UVa水题 ,紫书第三章例题1、2、3,UVa 272、10082、401, 提交ac了的
- 正则表达式学习参考
- Java Explicit Locks
- c++生成不重复的随机整数
- 自信的来源是什么_芮成钢《三十而立
- HDU 2874 — Connections between cities
- 正则表达式30分钟入门教程
- 牛叉本命年
- 电阻的命名含义
- UVa 12569 Planning mobile robot on Tree
- 推荐一本书---高质量c++/c编程指南