Previous Article: Java thread (6)
Lock is the interface under the java. util. Concurrent. Locks package,LockThe implementation providesSynchronizedMore extensive lock operations can be obtained by methods and statements. It can handle thread synchronization problems in a more elegant way. We use Java threads (2) the sample code is as follows:
Public class locktest {public static void main (string [] ARGs) {final outputter1 output = new outputter1 (); New thread () {public void run () {output. output ("zhangsan ");};}. start (); New thread () {public void run () {output. output ("Lisi ");};}. start () ;}} class outputter1 {private lock = new reentrantlock (); // Lock Object public void output (string name) {// todo thread output method lock. lock (); // get the lock try {for (INT I = 0; I <name. length (); I ++) {system. out. print (name. charat (I) ;}} finally {lock. unlock (); // release the lock }}}
This achieves the same synchronization effect as sychronized. Note that the lock is automatically released after the code is executed using the sychronized modification method or statement block, instead, we need to manually release the lock to use the lock. To ensure that the lock is eventually released (in case of exceptions), we need to put the mutex in try and release the lock in finally.
If this is lock, it cannot be a perfect solution to synchronization problems. The following describes readwritelock, which we will have a requirement, when reading and writing data, in order to ensure data consistency and integrity, read and write are mutually exclusive, and write are mutually exclusive, however, read and read do not need to be mutually exclusive. In this way, read and read are not mutually exclusive. Let's look at the code prototype that does not consider mutex:
Public class readwritelocktest {public static void main (string [] ARGs) {final data Data = new data (); For (INT I = 0; I <3; I ++) {New thread (New runnable () {public void run () {for (Int J = 0; j <5; j ++) {data. set (new random (). nextint (30 ));}}}). start () ;}for (INT I = 0; I <3; I ++) {New thread (New runnable () {public void run () {for (Int J = 0; j <5; j ++) {data. get ();}}}). start () ;}} class data {private int data; // shared data public void set (INT data) {system. out. println (thread. currentthread (). getname () + "prepare to write data"); try {thread. sleep (20);} catch (interruptedexception e) {e. printstacktrace ();} This. data = data; system. out. println (thread. currentthread (). getname () + "write" + this. data);} public void get () {system. out. println (thread. currentthread (). getname () + "prepare to read data"); try {thread. sleep (20);} catch (interruptedexception e) {e. printstacktrace ();} system. out. println (thread. currentthread (). getname () + "read" + this. data );}}
Some output results:
Thread-1 prepare to write data thread-3 prepare to read data thread-2 prepare to write data thread-0 prepare to write data thread-4 prepare to read data thread-5 prepare to read data thread-2 Write reading 12thread-4 reading 12thread-5 reading 5thread-1 writing 12
The write and write operations are mutually exclusive. The read and write operations are mutually exclusive. The sychronized modifier is added to the set and get methods:
public synchronized void set(int data) {...}public synchronized void get() {...}
Some output results:
Thread-0 prepare to write data thread-0 write 9thread-5 prepare to read data thread-5 read 9thread-5 prepare to read data thread-5 read 9thread-5 prepare to read data thread-5 read 9thread-5 prepare to read data thread- 5 read 9
Although the write and write operations are mutually exclusive, the read and write operations are mutually exclusive, but the read and read operations are also mutually exclusive. Concurrent execution is not allowed and the efficiency is low. Use the read/write lock to implement the Code as follows:
Class data {private int data; // shared data private readwritelock RWL = new reentrantreadwritelock (); Public void set (INT data) {RWL. writelock (). lock (); // get the write lock try {system. out. println (thread. currentthread (). getname () + "prepare to write data"); try {thread. sleep (20);} catch (interruptedexception e) {e. printstacktrace ();} This. data = data; system. out. println (thread. currentthread (). getname () + "write" + this. data);} finally {RWL. writelock (). unlock (); // release write lock} public void get () {RWL. readlock (). lock (); // get the read lock try {system. out. println (thread. currentthread (). getname () + "prepare to read data"); try {thread. sleep (20);} catch (interruptedexception e) {e. printstacktrace ();} system. out. println (thread. currentthread (). getname () + "read" + this. data);} finally {RWL. readlock (). unlock (); // release the read lock }}}
Some output results:
Thread-4 prepare to read data thread-3 prepare to read data thread-5 prepare to read data thread-5 read 18thread-4 read 18thread-3 read 18thread-2 prepare to write data thread-2 Write 6thread-2 prepare to write data thread- 2 Write 10thread-1 prepare write data thread-1 Write 22thread-5 prepare read data
We can see from the results that our requirements are met. This is only the basic usage of the lock, and the lock mechanism needs to be further studied.
Next article: Java threads (8)
This article is from: Gao Shuang | coder, original address: http://blog.csdn.net/ghsau/article/details/7461369.