It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
1.Lock is more object-oriented than the traditional threading model of synchronized, and the lock itself should be an object, similar to a lock in life. Code fragments executed by two threads to achieve the effect of a synchronous mutex, they must use the same lock object.
2. Read and write Lock: divided into read lock and write lock, multiple read lock is not mutually exclusive, read lock and write lock mutually exclusive, this is controlled by the JVM itself, you only need to good the corresponding lock. If your code is read-only and can be read by a lot of people at the same time, but not at the same time, read the lock; If your code modifies the data, only one person is writing and cannot read at the same time, write the lock. In short, read the time to read the lock, write the time to write the lock.
Example program:
Package edu.java5.lock;
Import Java.util.Random;
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock;
Import Java.util.concurrent.locks.ReentrantReadWriteLock;
public class Readwritelock {/** * Read and write locks: divided into read and write locks, multiple read locks are not mutually exclusive, read lock and write lock mutex, which is controlled by the JVM itself, you just need to good the corresponding lock.
* If your code is read-only and can be read by many people at the same time, but not at the same time, read the lock; * If your code modifies the data, only one person is writing and cannot read at the same time, write the lock.
* In short, read the time to read the lock, write the time on the write lock.
* Topic Requirements: * Three threads read data, three threads write data, write data can not read data, read data while other threads can read data but not write data; **/public static void Main (string[] args) {
Final Dataoperate dataoperate = new Dataoperate ();
for (int i = 0; i < 3; i++) {New Thread (new Runnable () {@Override public void run () {while (true) {
Dataoperate.get ();
}}). Start (); New Thread (New Runnable () {@Override public void run () {while (true) {Dataoperate.put (New Random (). Nextint
(1000));
}}). Start (); }}}} class dataoperate{Object data = null;//shared data, only one thread can write that data, but there can beMultiple threads read the data reentrantreadwritelock lock = new Reentrantreadwritelock ();
public void Get () {Lock.readlock (). Lock ();
System.out.println (Thread.CurrentThread (). GetName () + "ready to read data ...");
try {thread.sleep (1000);
} catch (Interruptedexception e) {e.printstacktrace ();
} System.out.println (Thread.CurrentThread (). GetName () + "read data" +data);
Lock.readlock (). Unlock ();
} public void put (int data) {Lock.writelock (). Lock ();
System.out.println (Thread.CurrentThread (). GetName () + "prepare to write data");
try {thread.sleep (1000);
} catch (Interruptedexception e) {e.printstacktrace ();
} this.data = data;
System.out.println (Thread.CurrentThread (). GetName () + "already written data" +data);
Lock.writelock (). Unlock (); }
}