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
- Public class Loggingwidget extends widget{
- Public static void Main (string[] args) {
- Loggingwidget lw=New Loggingwidget ();
- Lw.dosomething ();
- }
- Public synchronized void DoSomething () {
- System.out.println ("loggingwidget->dosomething ()");
- Doanotherthing (); //Call other synchronized methods in your own class
- super.dosomething (); //Call the Synchronized method of the parent class
- }
- Private synchronized void doanotherthing () {
- System.out.println ("loggingwidget->doanotherthing ()");
- }
- }
- Class widget{
- Public synchronized void DoSomething () {
- System.out.println ("widget->dosomething ()");
- }
- }
The execution results are:
[Java]View plain copy
- Loggingwidget->dosomething ()
- Loggingwidget->doanotherthing ()
- 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
- Public class reentry{
- int i=0;
- public static void Main (string[] args) {
- Reentry lw=new reentry ();
- Lw.dosomething ();
- }
- Public synchronized void DoSomething () {
- System.out.println ("dosomething ()" +i++);
- DoSomething (); //synchronized method Recursive invocation
- }
- }
Output Result:
.
[Java]View plain copy
- .....
- DoSomething ()3831
- DoSomething ()3832
- DoSomething ()3833
- Exception in thread "main" Java.lang.StackOverflowError
- ......
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
- Public class Loggingwidget {
- static public int i=0;
- public int ii=0;
- Public Loggingwidget () {
- super ();
- }
- Public static void Main (string[] args) {
- int totalnumofthread=20; //There are 20 threads executing at the same time
- Loggingwidget lw=New Loggingwidget (); //Each thread is associated with the same Loggingwidget object
- arraylist<outer> o=New arraylist<outer> ();
- for (int s=0;s<totalnumofthread;s++) //Assign a reference to the same Loggingwidget object for 20 threads
- {
- Outer t=new outer ();
- T.LW=LW;
- O.add (t);
- }
- For (int s=0;s<totalnumofthread;s++)
- {
- New Thread ((outer) o.get (s)). Start (); //Start 20 threads
- }
- }
- Public void DoSomething () { //Note, no method synchronized property is given here
- int sleep= (int) (Math.random () *500); //Randomly generates a sleep time
- try {
- Thread.Sleep (sleep); //Sleep
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- ii=i++; //Give each thread a id,id self-increment
- try {
- Thread.Sleep (sleep); //Continue to sleep
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- System.out.println (ii+"Thread Execution loggingwidget->dosomething (), Sleep Time:" +sleep);
- }
- }
- Class outer extends Thread
- {
- Public loggingwidget LW;
- Public outer () {
- super ();
- }
- @Override
- public Void Run () {
- Lw.dosomething ();
- Super.run ();
- }
- }
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)