Synchronization between Java and different methods of a class

Source: Internet
Author: User

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.

  1. Public class Readwritelockdemo {
  2. static SimpleDateFormat SDF = new SimpleDateFormat ("yyyy-mm-dd HH:mm:ss");
  3. public static void main (string[] Args) {
  4. Data data = New Data ();
  5. Worker T1 = new worker (data,true);
  6. Worker t2 = new worker (data,true);
  7. T1.start ();
  8. T2.start ();
  9. }
  10. static class Worker extends Thread {
  11. Data data;
  12. Boolean read;
  13. public Worker (data data, boolean read) {
  14. This.data = data;
  15. This.read = read;
  16. }
  17. public void run () {
  18. if (read)
  19. Data.get ();
  20. Else
  21. Data.set ();
  22. }
  23. }
  24. static class Data {
  25. Readwritelock lock = new Reentrantreadwritelock ();
  26. Lock Read = Lock.readlock ();
  27. Lock Write = Lock.writelock ();
  28. public void set () {
  29. Write.lock ();
  30. System.out.println (thread.currentthread (). hashcode ()
  31. + "set:begin" + sdf.format (new Date ()));
  32. try {
  33. Thread.Sleep (5000);
  34. //  
  35. } catch (Exception e) {
  36. } finally {
  37. System.out.println (thread.currentthread (). hashcode () + "set:end"
  38. + Sdf.format (new Date ()));
  39. Write.unlock ();
  40. }
  41. }
  42. public int get () {
  43. Read.lock ();
  44. System.out.println (thread.currentthread (). hashcode ()
  45. + "get:begin" + sdf.format (new Date ()));
  46. try {
  47. Thread.Sleep (5000);
  48. //  
  49. } catch (Exception e) {
  50. } finally {
  51. System.out.println (thread.currentthread (). hashcode () + "get:end"
  52. + Sdf.format (new Date ()));
  53. Read.unlock ();
  54. }
  55. return 1;
  56. }
  57. }
  58. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.