IllegalMonitorStateException in code

前端 未结 3 740
南笙
南笙 2021-01-16 10:32
class Test {

    public static void main(String[] args) {

        System.out.println(\"1.. \");
        synchronized (args) {

            System.out.println(\"2..         


        
3条回答
  •  臣服心动
    2021-01-16 11:00

    Hi ankit i assume you are trying to learn some basic concepts of multithreading. try to get hold of some good online tutorials :http://www.javaworld.com/jw-04-1996/jw-04-threads.html

    or try some basic good book. http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060/

    The program you wrote actually doesn't need synchronization as there is only one thread (main). I know you are just trying your hands, therefore giving some insights. Even if you correctly called wait method on args(args.wait()) or synchronized on Thread.currentThread your thread may goes into indefinite wait (making your program unresponsive) because there is no other thread to notify your main thread.

提交回复
热议问题