Reusability of Java Internal locks (reentrancy)

Source: Internet
Author: User

Java provides an internal locking mechanism that enforces atomicity: synchronized blocks. But an internal lock is reentrant, and the request succeeds when the thread tries to acquire a lock that it itself occupies.

Simply put, you can always get the lock when you call the other synchronized methods of this class inside a synchronized method.

As shown in example 1.1

[Java]View plain copy
  1. Public class Loggingwidget extends widget{
  2. Public static void Main (string[] args) {
  3. Loggingwidget lw=New Loggingwidget ();
  4. Lw.dosomething ();
  5. }
  6. Public synchronized void DoSomething () {
  7. System.out.println ("loggingwidget->dosomething ()");
  8. Doanotherthing (); //Call other synchronized methods in your own class
  9. super.dosomething (); //Call the Synchronized method of the parent class
  10. }
  11. Private synchronized void doanotherthing () {
  12. System.out.println ("loggingwidget->doanotherthing ()");
  13. }
  14. }
  15. Class widget{
  16. Public synchronized void DoSomething () {
  17. System.out.println ("widget->dosomething ()");
  18. }
  19. }

The execution results are:

[Java]View plain copy
    1. Loggingwidget->dosomething ()
    2. Loggingwidget->doanotherthing ()
    3. Widget->dosomething ()


As you can see, inside Java, calling the Synchronized method of the parent class and calling the other synchronized methods in your own class will not hinder the program's running, because the Java thread is based on "per-thread (per-thread)" rather than " Per-Call (per-invocation) ". the implementation of the re-entry is by associating a request count for each lock and a thread that occupies it .

What's more, there are infinite recursion situations:

[Java]View plain copy
  1. Public class reentry{
  2. int i=0;
  3. public static void Main (string[] args) {
  4. Reentry lw=new reentry ();
  5. Lw.dosomething ();
  6. }
  7. Public synchronized void DoSomething () {
  8. System.out.println ("dosomething ()" +i++);
  9. DoSomething (); //synchronized method Recursive invocation
  10. }
  11. }


Output Result:
.

[Java]View plain copy
    1. .....
    2. DoSomething ()3831
    3. DoSomething ()3832
    4. DoSomething ()3833
    5. Exception in thread "main" Java.lang.StackOverflowError
    6. ......


As you can see, the program enters a dead loop until the stack overflows. But the lock is not released.
=============================================================
Remember that the " re-entry implementation is by associating a request count with each lock and a thread that occupies it ", meaning that if there are other threads calling a method of the object, then the other synchronized methods of the object cannot be re-entered, but mutually exclusive, Because the process that occupies the function is different, see the following example:

[Java]View plain copy
  1. Public class Loggingwidget {
  2. static public int i=0;
  3. public int ii=0;
  4. Public Loggingwidget () {
  5. super ();
  6. }
  7. Public static void Main (string[] args) {
  8. int totalnumofthread=20; //There are 20 threads executing at the same time
  9. Loggingwidget lw=New Loggingwidget (); //Each thread is associated with the same Loggingwidget object
  10. arraylist<outer> o=New arraylist<outer> ();
  11. for (int s=0;s<totalnumofthread;s++) //Assign a reference to the same Loggingwidget object for 20 threads
  12. {
  13. Outer t=new outer ();
  14. T.LW=LW;
  15. O.add (t);
  16. }
  17. For (int s=0;s<totalnumofthread;s++)
  18. {
  19. New Thread ((outer) o.get (s)). Start (); //Start 20 threads
  20. }
  21. }
  22. Public void DoSomething () { //Note, no method synchronized property is given here
  23. int sleep= (int) (Math.random () *500); //Randomly generates a sleep time
  24. try {
  25. Thread.Sleep (sleep); //Sleep
  26. } catch (Interruptedexception e) {
  27. E.printstacktrace ();
  28. }
  29. ii=i++; //Give each thread a id,id self-increment
  30. try {
  31. Thread.Sleep (sleep); //Continue to sleep
  32. } catch (Interruptedexception e) {
  33. E.printstacktrace ();
  34. }
  35. System.out.println (ii+"Thread Execution loggingwidget->dosomething (), Sleep Time:" +sleep);
  36. }
  37. }
  38. Class outer extends Thread
  39. {
  40. Public loggingwidget LW;
  41. Public outer () {
  42. super ();
  43. }
  44. @Override
  45. public Void Run () {
  46. Lw.dosomething ();
  47. Super.run ();
  48. }
  49. }

Because the method synchronized attribute is not given, the output is as follows:
1 Thread Execution loggingwidget->dosomething (), Sleep time: 4
1 Thread Execution loggingwidget->dosomething (), Sleep time: 4
3 Thread Execution loggingwidget->dosomething (), Sleep time: 56
8 Thread Execution loggingwidget->dosomething (), Sleep time: 108
11 Thread Execution loggingwidget->dosomething (), Sleep time: 151
11 Thread Execution loggingwidget->dosomething (), Sleep time: 157
12 Thread Execution loggingwidget->dosomething (), Sleep time: 177
13 Thread Execution loggingwidget->dosomething (), Sleep time: 192
14 Thread Execution loggingwidget->dosomething (), Sleep time: 213
16 Thread Execution loggingwidget->dosomething (), Sleep time: 218
17 Thread Execution loggingwidget->dosomething (), Sleep time: 232
19 Thread Execution loggingwidget->dosomething (), Sleep time: 280
19 Thread Execution loggingwidget->dosomething (), Sleep time: 354
19 Thread Execution loggingwidget->dosomething (), Sleep time: 358
19 Thread Execution loggingwidget->dosomething (), Sleep time: 401
19 Thread Execution loggingwidget->dosomething (), Sleep time: 428
19 Thread Execution loggingwidget->dosomething (), Sleep time: 437
19 Thread Execution loggingwidget->dosomething (), Sleep time: 455
19 Thread Execution loggingwidget->dosomething (), Sleep time: 468
19 Thread Execution loggingwidget->dosomething (), Sleep time: 498
If the methodPublic void DoSomething ()Change intoPublic synchronized void dosomething (),Then the output is:
0 Thread Execution loggingwidget->dosomething (), Sleep time: 384
1 Thread Execution loggingwidget->dosomething (), Sleep time: 26
2 thread Execution loggingwidget->dosomething (), Sleep time: 391
3 Thread Execution loggingwidget->dosomething (), Sleep time: 289
4 Thread Execution loggingwidget->dosomething (), Sleep time: 266
5 Thread Execution loggingwidget->dosomething (), Sleep time: 248
6 Thread Execution loggingwidget->dosomething (), Sleep time: 121
7 Thread Execution loggingwidget->dosomething (), Sleep time: 395
8 Thread Execution loggingwidget->dosomething (), Sleep time: 454
9 Thread Execution loggingwidget->dosomething (), Sleep time: 457
10 Thread Execution loggingwidget->dosomething (), Sleep time: 181
11 Thread Execution loggingwidget->dosomething (), Sleep time: 170
12 Thread Execution loggingwidget->dosomething (), Sleep time: 470
13 Thread Execution loggingwidget->dosomething (), Sleep time: 444
14 Thread Execution loggingwidget->dosomething (), Sleep time: 114
15 Thread Execution loggingwidget->dosomething (), Sleep time: 4
16 Thread Execution loggingwidget->dosomething (), Sleep time: 40
17 Thread Execution loggingwidget->dosomething (), Sleep time: 320
18 Thread Execution loggingwidget->dosomething (), Sleep time: 416
19 Thread Execution loggingwidget->dosomething (), Sleep time: 148
It can be seen that the synchronized method of invoking the same object by different threads is not re-entered, but instead creates a mutex.

Transferred from: http://blog.csdn.net/haydenwang8287/article/details/5837734

Reusability of Java Internal locks (reentrancy)

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.