Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Deadlock!!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Deadlock!!

    Hi, I am trying to fix the deadlock situation here. Each Thread is waiting for the other to exit the bow synchronized method and therefore bowback method can not be invoked. How can i fix this code so the deadlock does not occur.. I've been trying to fix this for ages. i really appreciate anyone's help. Thanks.
    //
    public class Deadlock {
        static class Friend {
            private final String name;
            public Friend(String name) {
                this.name = name;
            }
            public String getName() {
                return this.name;
            }
            public synchronized void bow(Friend bower) {
                System.out.format("%s: %s has bowed to me!%n", 
                        this.name, bower.getName());
                bower.bowBack(this);
            }
            public synchronized void bowBack(Friend bower) {
                System.out.format("%s: %s has bowed back to me!%n",
                        this.name, bower.getName());
            }
        }
     
        public static void main(String[] args) {
            final Friend alphonse = new Friend("Alphonse");
            final Friend gaston = new Friend("Gaston");
            new Thread(new Runnable() {
                public void run() { alphonse.bow(gaston); }
            }).start();
            new Thread(new Runnable() {
                public void run() { gaston.bow(alphonse); }
            }).start();
        }
    }
    //
    Last edited by helloworld922; October 12th, 2011 at 06:28 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Deadlock!!

    First, please wrap your code in the code tags....

    How can i fix this code so the deadlock does not occur
    Why are the methods synchronized? In the code posted, you are not changing any values, so there shouldn't be concurrency issues.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deadlock!!

    here is really no deadlock
    what is a real your problem please clarify more exactly



    __________________________________________________ ____________________________
    How to make a flash game?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Deadlock!!

    Quote Originally Posted by ofesak View Post
    here is really no deadlock
    what is a real your problem please clarify more exactly
    Really? No deadlock? Are you going to explain, or are you just here to post nonsense with links to drive traffic to your blog

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Deadlock!!

    I have a little hard to see why you want to sync this. The only reason I see is if you always want a "bowBack" before a new "bow" is performed. If this is the case I should synchronize "bow" with an class variable like this:

    static class Friend {
            private static final Object syncObject = new Object();
            private final String name;
            public Friend(String name) {
                this.name = name;
            }
            public String getName() {
                return this.name;
            }
            public void bow(Friend bower) {
                synchronized(syncObject) {
                    System.out.format("%s: %s has bowed to me!%n",
                            this.name, bower.getName());
                    bower.bowBack(this);
                }
            }
            public void bowBack(Friend bower) {
                System.out.format("%s: %s has bowed back to me!%n",
                        this.name, bower.getName());
            }
        }

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deadlock!!

    Quote Originally Posted by copeg View Post
    Really? No deadlock? Are you going to explain, or are you just here to post nonsense with links to drive traffic to your blog
    This is nonsense? explain please why? I am just asking a question about deadlock as i am new to threads and synchronization. This was a problem i was given as part of an assignment. I know the methods do not need to be synchronized.. it's the first thing i noticed.. but in the case that they really did, how i would fix this. Thanks alot qurtan for a solution for this..

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Deadlock!!

    Quote Originally Posted by mulligan252 View Post
    This is nonsense? explain please why?
    Copeg wasn't referring to your topic, he was referring to ofesak's post about there not being any deadlock.

    There is absolutely the potential for a deadlock problem here. The most obvious solution is to remove synchronization as there's no data dependency issues, but without knowing if you wanted some sort of execution order to be enforced there's no way to tell if this is the correct solution.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    copeg (October 16th, 2011)

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Deadlock!!

    This is nonsense? explain please why?
    The comment was in reference to the quoted text in my post - a quote from the post by ofesak - not to your post mulligan252 (thank you helloworld for the clarification)

Similar Threads

  1. How to avoid the Deadlock in the below program
    By murali1253 in forum Threads
    Replies: 0
    Last Post: April 15th, 2010, 05:35 PM