1.Lock
java.util.concurrent.locks.Lock
接口。主要方法有lock()与unlock()。
2.ReentrantLock
java.util.concurrent.locks.ReentrantLock
继承自lock。
- //可重入锁的可重入场景
- recursiveFun(){
- lock.lock();
- recursiveFun();
- lock.unlock();
- }
3. ReadWriteLock
java.util.concurrent.locks.ReadWriteLock
ReadWriteLock使得你可以同时有多个读者,只要它们都不试图写入即可。如果写锁已经被其他任务持有,那么任何读者都不能访问,也不能再次写入,直到这个写锁被释放为止。
java.util.concurrent.locks.ReentrantReadWriteLock. ReentrantReadWriteLock (boolean fair)
构造函数。
Creates a new ReentrantReadWriteLock with the given fairness policy.
WriteLock java.util.concurrent.locks.ReentrantReadWriteLock. writeLock ()
返回write锁。
Returns the lock used for writing.
ReadLock java.util.concurrent.locks.ReentrantReadWriteLock. readLock ()
返回read锁。
Returns the lock used for reading.
int java.util.concurrent.locks.ReentrantReadWriteLock. getReadLockCount ()
返回该lock对象的read 锁的个数。
Queries the number of read locks held for this lock.
//: concurrency/ReaderWriterList.javaimport java.util.concurrent.* ;import java.util.concurrent.locks.* ;import java.util.* ;import static net . mindview . util . Print .*;public class ReaderWriterList < T > {private ArrayList < T > lockedList ;// Make the ordering fair:private ReentrantReadWriteLock lock =new ReentrantReadWriteLock ( true );public ReaderWriterList ( int size , T initialValue ) {lockedList = new ArrayList < T >(Collections . nCopies ( size , initialValue ));}public T set ( int index , T element ) {Lock wlock = lock . writeLock ();wlock . lock ();try {return lockedList . set ( index , element );} finally {wlock . unlock ();}}public T get ( int index ) {Lock rlock = lock . readLock ();rlock . lock ();try {// Show that multiple readers// may acquire the read lock:if ( lock . getReadLockCount () > 1 )print ( lock . getReadLockCount ());return lockedList . get ( index );} finally {rlock . unlock ();}}public static void main ( String [] args ) throws Exception {new ReaderWriterListTest ( 3 , 1 );}}class ReaderWriterListTest {ExecutorService exec = Executors . newCachedThreadPool ();private final static int SIZE = 10 ;private static Random rand = new Random ( 47 );private ReaderWriterList < Integer > list =new ReaderWriterList < Integer >( SIZE , 0 );private class Writer implements Runnable {public void run () {try {for ( int i = 0 ; i < 5 ; i ++) {list . set ( i , rand . nextInt ());TimeUnit . MILLISECONDS . sleep ( 10 );}} catch ( InterruptedException e ) {// Acceptable way to exit}print ( "Writer finished, shutting down" );exec . shutdownNow ();}}private class Reader implements Runnable {public void run () {try {while (! Thread . interrupted ()) {for ( int i = 0 ; i < SIZE ; i ++) {list . get ( i );TimeUnit . MILLISECONDS . sleep ( 1 );}}} catch ( InterruptedException e ) {// Acceptable way to exit}}}public ReaderWriterListTest ( int readers , int writers ) {for ( int i = 0 ; i < readers ; i ++)exec . execute ( new Reader ());for ( int i = 0 ; i < writers ; i ++)exec . execute ( new Writer ());}} /*(每次都会变)322222Writer finished, shutting down*/