Results 1 to 9 of 9
  1. #1
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default attempt to obtain deadlock statement

    Hello!
    I have such program:

    Java Code:
    class Person implements Runnable
    {
        private String name;
        public Person friend;
        public Integer age=30;
        public Object o;
    
        public Person(String name) {
            this.name = name;
        }
    
        public void say(String word)
        {
            System.out.println(word);
        }
    
        public synchronized void bow(Person friend) throws InterruptedException {
            //perform bow
            System.out.println(this.name+" bow to "+friend.name);
            this.wait();
            //age--;
            System.out.println(age);
            friend.waveAHand(friend);
        }
    
        private synchronized void waveAHand(Person friend)
        {
            //wave a hand
            System.out.println(this.name+" wave to "+friend.name);
        }
    
        @Override
        public void run() {
            try {
                bow(friend);
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    	Person A = new Person("A");
            Person B = new Person("B");
            A.friend = B;
            B.friend = A;
            A.run();
            Thread.sleep(1000);
            B.run();
            Thread.sleep(1000);
            A.notify();
            B.notify();
        }
    }
    But somehow the main thread freezes forever while it runs A object. Why does it happen?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default Re: attempt to obtain deadlock statement

    Don't use run(), instead use start() to fire off a new Thread.

    Edit: since Person implements Runnable and is not a Thread, you need to create a Thread that runs your code:
    new Thread(A).start();
    Last edited by SurfMan; 12-15-2013 at 07:43 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: attempt to obtain deadlock statement

    the main thread freezes forever while it runs A object
    The wait() at line 20 stops the thread that is executing it. Since that is the only thread that is running, the program waits.
    See SurfMan's solution.

    You will also have to consider the proper way to call the notify() method. See the API doc for the Object class for a description of what needs to be done.
    Last edited by Norm; 12-15-2013 at 07:56 PM.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: attempt to obtain deadlock statement

    Quote Originally Posted by Norm View Post
    The wait() at line 20 stops the thread that is executing it. Since that is the only thread that is running, the program waits.
    See SurfMan's solution.

    You will also have to consider the proper way to call the notify() method. See the API doc for the Object class for a description of what needs to be done.
    yeah, i have realized that main thread don't own the monitor(by none of 3 possible ways). I rewrite main thread code so it looks like this:

    Java Code:
    	    Person A = new Person("A");
            Person B = new Person("B");
            A.friend = B;
            B.friend = A;
    
            Thread Ath = new Thread(A);
                    Ath.start();
            Thread.sleep(1000);
            Thread Bth = new Thread(B);
                    Bth.start();
            Thread.sleep(1000);
            A.resume();
            B.resume();
    and implemented new method in class A :

    Java Code:
        public synchronized void resume()
        {
            this.notify();
        }
    So now , no exceptions occur. But JVM writes the next :
    Hot Swap failed
    Main: add method not implemented;
    Main: Operation not supported by VM
    Unfortunately google can not help me figure out this phrase.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default Re: attempt to obtain deadlock statement

    You get that message from your IDE and it's telling you your program is already running, but it can't reload the classes in memory since the signature changed. Stop the program and restart. This is not a Java problem but an IDE notification.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: attempt to obtain deadlock statement

    Hot Swap failed
    Is that error from the IDE you are using? The threads in your program don't all stop when the program exits so some of them are still running. The IDE tries to "Hot Swap" the changes you made with the code in the executing threads and had a problem.
    Possible solution: kill the JVM before starting another test.
    Or add this at the end of the main() method:
    Java Code:
            System.out.println("main exiting");
            Thread.sleep(5000);    // allow program to do its thing
            System.exit(0);          //  close everything
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default Re: attempt to obtain deadlock statement

    Quote Originally Posted by Norm View Post
    Is that error from the IDE you are using? The threads in your program don't all stop when the program exits so some of them are still running. The IDE tries to "Hot Swap" the changes you made with the code in the executing threads and had a problem.
    Possible solution: kill the JVM before starting another test.
    Or add this at the end of the main() method:
    Java Code:
            System.out.println("main exiting");
            Thread.sleep(5000);    // allow program to do its thing
            System.exit(0);          //  close everything
    Ok. But what "hot swap" phrase means ?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: attempt to obtain deadlock statement

    Try an Internet search engine.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    990
    Rep Power
    2

    Default Re: attempt to obtain deadlock statement

    Hot swap is the process of replacing old code with new code in memory. That is currently only possibe when the body of methods etc change, not on signature changes.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Deadlock with DocumentListener
    By dsharma in forum AWT / Swing
    Replies: 6
    Last Post: 04-30-2012, 03:30 PM
  2. Compiler deadlock
    By Arnold in forum New To Java
    Replies: 2
    Last Post: 06-27-2010, 12:57 PM
  3. How to avoid the Deadlock in the below program
    By murali1253 in forum New To Java
    Replies: 3
    Last Post: 04-16-2010, 05:46 PM
  4. [SOLVED] Deadlock ?
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-14-2009, 07:54 PM
  5. Thread Deadlock
    By ajeeb in forum New To Java
    Replies: 2
    Last Post: 01-16-2009, 02:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •