Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:04:31+00:00 2026-06-11T10:04:31+00:00

I’ve found that including a call to System.out.format in the classic Java Deadlock Tutorial

  • 0

I’ve found that including a call to System.out.format in the classic Java Deadlock Tutorial will prevent deadlock from occurring, and I can’t figure out why.

The code below is the same as that of the tutorial, with the addition to main of System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());

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) throws InterruptedException {
        final Friend alphonse = new Friend("Alphonse");
        final Friend gaston = new Friend("Gaston");

        System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());

        new Thread(new Runnable() {
            public void run() { alphonse.bow(gaston); }
        }).start();

        new Thread(new Runnable() {
            public void run() { gaston.bow(alphonse); }
        }).start();
    }
}

Here’s the output:

Hi, I'm Alphonse...no deadlock for you!

Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed back to me!
Gaston: Alphonse has bowed to me!
Alphonse: Gaston has bowed back to me!

Removing the offending line results in the usual deadlock:

Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed to me!
... deadlock ...

Is the call to System.out.format somehow changing the way the threads acquire the intrinsic locks on the objects?

Update:

I was able to get the system to deadlock again just by changing where I start the threads in the code:

public static void main(String[] args) throws InterruptedException {
    final Friend alphonse = new Friend("Alphonse");
    final Friend gaston = new Friend("Gaston");

    System.out.format("Hi, I'm %s...no deadlock for you!\n\n", alphonse.getName());

    Thread t1 = new Thread(new Runnable() {
        public void run() { alphonse.bow(gaston); }
    });

    Thread t2 = new Thread(new Runnable() {
        public void run() { gaston.bow(alphonse); }
    });

    t1.start();
    t2.start();
}

This begs the question about how we can get greater insight into how the thread scheduler behaves, but I’ll save that for a different day.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:04:32+00:00Added an answer on June 11, 2026 at 10:04 am

    You did not really remove the deadlock but rather (because of some internal JVM reason) changed that timing of the threads so that one of the threads enters bowBack() before the other calls bow().
    Just put in bow: sleep(1000) and your deadlock will reappear.

    Note that deadlock does not always happen, only when the threads are in lucky timing. In this case, deadlock will happen when both threads enter bow and before any of them call bowBack

    …
    And “some internal JVM reason” can be the following:

    In your case there are actually three threads: the one executing main, t1, and t2.
    The reason why putting print hides the deadlock can be that the thread scheduler decided that main still had work to do, i.e. flushing the io buffers, and therefore let main continue after starting t1 and before starting t2. If you are on a dual-core cpu, only main and t1 would run, but t2 would wait, since print is a slow operation. Context switching would take some more time, and t1 would finish before t2 could start…so no deadlock would happen. But that does not mean that if you run the program again deadlock won’t happen.

    If you want to play, create a queue and push tokens (thread names) in that queue, then join your threads in main. After they finish, print the queue content and you can observe the timing of the threads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.