I have developed the program to prove that if thread and it is in static synchronized method still a thread can enter other static synchronized method from that method in java itself but my programs throws the exception please advise what went wrong in it , below is the code
package com.synchrnozie;
class MyRunnable18 implements Runnable {
@Override
public void run() {
method1();
}
static synchronized void method1() {
System.out.println("static synchronized void method1() started");
method2();
System.out.println("static synchronized void method1() ended");
}
static synchronized void method2() {
System.out.println("in static synchronized method2()");
method3();
}
static synchronized void method3() {
System.out.println("in static synchronized method3()");
method3();
}
}
public class staticNesting {
public static void main(String args[]) throws InterruptedException {
MyRunnable18 MyRunnable18 = new MyRunnable18();
Thread thread1 = new Thread(MyRunnable18, "Thread-1");
thread1.start();
}
}
and below is the exception stack trace ..
Exception in thread "Thread-1" java.lang.StackOverflowError
at sun.nio.cs.SingleByte.withResult(Unknown Source)
at sun.nio.cs.SingleByte.access$000(Unknown Source)
at sun.nio.cs.SingleByte$Encoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.SingleByte$Encoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at com.synchrnozie.MyRunnable18.method3(staticNesting.java:24)
at com.synchrnozie.MyRunnable18.method3(staticNesting.java:25)