Lock - java-util-concurrent

来源:互联网 发布:大数据对零售业 编辑:程序博客网 时间:2024/06/06 04:39

Lock

Jakob Jenkov
Last update: 2014-06-23                              

A java.util.concurrent.locks.Lock is a thread synchronization mechanism 【机制】 just like synchronized blocks. A Lock is, however, more flexible 【灵活的】 and more sophisticated 【复杂的】 than a synchronized block.

By the way, in my Java Concurrency tutorial I have described how to implement your own locks, in case you are interested (or need it). See my text on Locks for more details.


译文:

java.util.concurrent.locks.Lock 就像synchronized 块一样是一种线程同步机制。

然而Lock比synchronized 块更精细更复杂。

顺便提一下,在我的 Java Concurrency tutorial  中我已经说明了如何去实现你自己的锁。如果你有所兴趣或者需要,接着看更多关于Lock 的细节。

Java Lock Example

Since Lock is an interface, you need to use one of its implementations to use a Lock in your applications. Here is a simple usage example:

Lock lock = new ReentrantLock();lock.lock();//critical sectionlock.unlock();

First a Lock is created. Then it's lock() method is called. Now the Lock instance is locked. Any other thread calling lock() will be blocked until the thread that locked the lock calls unlock(). Finally unlock() is called, and the Lock is now unlocked so other threads can lock it.


译文:

//critical section 临界区

首先创建一个Lock 。然后调用lock()方法。现在这个Lock 示例已经被锁定了。

其他任何的线程尝试去调用lock()方法都会阻塞,除非调用lock()方法的线程调用了unlock()方法释放了锁。

最后调用unlock()方法释放锁,其他线程可以调用lock()方法锁定一个示例 。


Java Lock Implementations

The java.util.concurrent.locks package has the following implementations of the Lock interface:

  • ReentrantLock

Main Differences Between Locks and Synchronized Blocks

The main differences between a Lock and a synchronized block are:

  • A synchronized block makes no guarantees about the sequence in which threads waiting to entering it are granted access.
  • You cannot pass any parameters to the entry of a synchronized block. Thus, having a timeout trying to get access to a synchronized block is not possible.
  • The synchronized block must be fully contained within a single method. A Lock can have it's calls to lock() and unlock() in separate methods.


译文:

Java Lock的实现

java.util.concurrent.locks 包有Lock接口的几个实现:
  • ReentrantLock  重入锁

Locks和synchronized 块的主要不同点:

  • synchronized 块无法保证等待进入它的线程的顺序。
  • 无法将任何参数传递到synchronized 块。因此,希望通过超时去访问一个synchronized 块是不可行的。
  • synchronized 块必须在一个方法中使用。Lock 可以在不同的方法中调用lock()和unlock()。

Lock Methods

The Lock interface has the following primary methods:

  • lock()
  • lockInterruptibly()
  • tryLock()
  • tryLock(long timeout, TimeUnit timeUnit)
  • unlock()

The lock() method locks the Lock instance if possible. If the Lock instance is already locked, the thread calling lock() is blocked until the Lock is unlocked.

The lockInterruptibly() method locks the Lock unless the thread calling the method has been interrupted. Additionally, if a thread is blocked waiting to lock the Lock via this method, and it is interrupted, it exits this method calls.

The tryLock() method attempts to lock the Lock instance immediately. It returns true if the locking succeeds, false if Lock is already locked. This method never blocks.

The tryLock(long timeout, TimeUnit timeUnit) works like the tryLock() method, except it waits up the given timeout before giving up trying to lock the Lock.

The unlock() method unlocks the Lock instance. Typically, a Lock implementation will only allow the thread that has locked the Lock to call this method. Other threads calling this method may result in an unchecked exception (RuntimeException).

Next: ReadWriteLock


译文:

Lock方法

The Lock interface has the following primary methods:

Lock接口的私有方法:

  • lock()
  • lockInterruptibly()
  • tryLock()
  • tryLock(long timeout, TimeUnit timeUnit)
  • unlock()

方法说明:
  • lock() :  如果获取到锁的话,lock()将会锁定Lock对象。如果Lock对象已经被锁了,调用lock()方法的线程将会阻塞直到Lock对象的锁被释放。
  • lockInterruptibly()    lockInterruptibly()锁定Lock对象除非调用 lockInterruptibly()方法的线程被中断。另外,如果一个线程在尝试获取Lock对象的锁的时候被中断了,它将会退出该方法。
  • tryLock()     tryLock() 方法尝试立即去获取Lock对象的锁,如果获取到锁返回true,如果Lock对象已经被(其他线程)锁返回false,这个方法不会阻塞。
  • tryLock(long timeout, TimeUnit timeUnit)   tryLock(long timeout, TimeUnit timeUnit) 方法与tryLock() 方法类似,该方法在放弃获取Lock对象的锁之前会等待timeout时间。
  • unlock() unlock()方法释放Lock对象的锁。通常情况下,通常指允许持有Lock对象的锁的线程去调用unlock()方法,其他线程调用此方法可能会抛出一个unchecked exception (RuntimeException).




0 0
原创粉丝点击