Home > Back-end >  Synchronized problem
Synchronized problem

Time:01-23

Want to test a synchronized object lock in the concept of "reentrant lock" operation error, please answer



 
Public class SynchronizedTest {
Public static void main (String [] args) {

MyThreadSub mt=new MyThreadSub ();
Thread the t1=new Thread (mt);
t1.start();
New Thread (new MyThreadMain ()). The start ();
}
}

The class MyThreadSub extends Thread {
@ Override
Public void the run () {
Sub Sub=new Sub ();
Sub. OperateISubMethod ();
}
}

The class MyThreadMain extends Thread {
@ Override
Public void the run () {
The Main Main=new Main ();
The main operateIMainMethod ();
}
}
The class Main
{
Private int I=10;
Public synchronized void operateIMainMethod ()
{
For (int i1=0; I1 & lt; 5; {i1 + +)
System. The out. Println (" main print I="+ i1);
}
}
}

The class Sub extends the Main
{
Private int j=10;
Public synchronized void operateISubMethod ()
{
For (int I=0; I & lt; 3; I++) {
System. The out. Println (" Sub print j="+ I);
System. The out. Println ("=============================");
Enclosing operateISubMethod ();
}
}
}

CodePudding user response:

The enclosing operateISubMethod ();
To this. OperateIMainMethod ();
Otherwise, operateISubMethod infinite recursion stack overflow its own making

CodePudding user response:

Sub. OperateISubMethod (); The infinite loop
  • Related