Once the synchronized adornment is added to the method of the object, only one thread can access the synchronized decorated method at any Time. Suppose that a data object has a write method and a read method, in order to ensure the security of data in multi-threaded environment, it is necessary to add the synchronized synchronization block to the Object's reading and writing METHODS. When any thread is written, other threads cannot read and change the data, and other threads cannot read or write while the threads are Reading. This way, when the write operation is much larger than the read operation, the problem is small, and when the read is far greater than the write, it can cause a performance bottleneck, because the read operation is possible at the same time, and the lock operation restricts the concurrent reading of the Data.
Readwritelock solves this problem, while the other threads cannot read or write the data while the write operation, while the other threads cannot write to the data while the read operation, but can read the Data.
- Public class Readwritelockdemo {
- static SimpleDateFormat SDF = new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss");
- public static void main (string[] Args) {
- Data data = New Data ();
- Worker T1 = new worker (data,true);
- Worker t2 = new worker (data,true);
- T1.start ();
- T2.start ();
- }
- static class Worker extends Thread {
- Data data;
- Boolean read;
- public Worker (data data, boolean read) {
- This.data = data;
- This.read = read;
- }
- public void run () {
- if (read)
- Data.get ();
- Else
- Data.set ();
- }
- }
- static class Data {
- Readwritelock lock = new Reentrantreadwritelock ();
- Lock Read = Lock.readlock ();
- Lock Write = Lock.writelock ();
- public void set () {
- Write.lock ();
- System.out.println (thread.currentthread (). hashcode ()
- + "set:begin" + sdf.format (new Date ()));
- try {
- Thread.Sleep (5000);
- //
- } catch (Exception e) {
- } finally {
- System.out.println (thread.currentthread (). hashcode () + "set:end"
- + Sdf.format (new Date ()));
- Write.unlock ();
- }
- }
- public int get () {
- Read.lock ();
- System.out.println (thread.currentthread (). hashcode ()
- + "get:begin" + sdf.format (new Date ()));
- try {
- Thread.Sleep (5000);
- //
- } catch (Exception e) {
- } finally {
- System.out.println (thread.currentthread (). hashcode () + "get:end"
- + Sdf.format (new Date ()));
- Read.unlock ();
- }
- return 1;
- }
- }
- }
Two threads are read threads and the results are as follows
22474382 Get:begin 2011-04-16 18:26:13
4699264 Get:begin 2011-04-16 18:26:13
22474382 get:end 2011-04-16 18:26:18
4699264 get:end 2011-04-16 18:26:18
Both read threads can read the data at the same time, the following is a read thread, a write thread case
Data data = new Data ();
Worker T1 = new worker (data,false);
Worker t2 = new worker (data,true);
T2.start ();
Thread.Sleep (100);
T1.start ();
Start the read thread before starting the write thread to see the results
14718739 Get:begin 2011-04-16 18:54:46
14718739 get:end 2011-04-16 18:54:51
14737862 Set:begin 2011-04-16 18:54:51
14737862 set:end 2011-04-16 18:54:56
You can see that the write thread is unable to access the data while the read thread is working
Synchronization between Java and different methods of a class