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

  • SEARCH
  • Home
  • 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 8867025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:55:43+00:00 2026-06-14T16:55:43+00:00

I’m developing a demo program that shows the impact of different ways of handling

  • 0

I’m developing a demo program that shows the impact of different ways of handling threads. I’m using a simple task of taking a char array and building a name sending each char to a StringBuilder which calls a runnable called NameBuilder which in turn uses a letterAdder class with synchronized methods and a static StringBuilder variable to hold the name as it is built. This structure is loosely based on the WaxoMatic example in “Thinking in Java” by Bruce Eckel

Here is the code that I am using for the notifyAll() method:

import java.util.List;
import java.util.concurrent.*;

class LetterAdder{
    public static StringBuilder currName = new StringBuilder();
    public static boolean busyAdding = false;

    public synchronized void addLetterSynchWithNotifyAll(char letter) throws InterruptedException{
        while (busyAdding){
            System.out.println("Letter " +letter+" has to wait");
            wait();
        }
        busyAdding=true;
        System.out.println("About to add " + letter + " - ");
        currName.append(letter);
        System.out.println("name is now " + currName);
        busyAdding=false;
    }

    public synchronized void doNotifyAll(char letter) throws InterruptedException{
        System.out.print(letter + " is notifying all");
        notifyAll();
    }
}

class NameBuilder implements Runnable{
    private LetterAdder adder = new LetterAdder();
    private char nextLetter;

    public NameBuilder(char nextLetter){
        adder = new LetterAdder();
        this.nextLetter=nextLetter;
    }

    public void run(){
        try{
            adder.addLetterSynchWithNotifyAll(nextLetter);
                adder.doNotifyAll(nextLetter);
        } catch (InterruptedException e) {
            System.out.println("Letter "+nextLetter+" interrupted!");
        }

        //tie up thread for specified time to ensure other threads go to wait 
        try{
            TimeUnit.SECONDS.sleep(1);
        } catch(Exception e){}
    }
}

public class NotifyDemo {
    public static void main(String[] args) {
        char[] chars = {'M', 'a','r','t','i','n'};
        ExecutorService pool = Executors.newCachedThreadPool();

        for (int i=0; i<chars.length; i++){
            NameBuilder builder = new NameBuilder(chars[i]);
            pool.execute(builder);
        }

        System.out.println("\nWill shutdown thread pool in 20 seconds");
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {}
        System.out.println("\nAbout to shutdown thread pool ");
        List<Runnable> tasks = pool.shutdownNow();
        if (!tasks.isEmpty()){
            for (Runnable r: tasks){
                System.out.println("Uncompleted task: "+r.toString());
            }
        }
    }
}


/** Output
About to add M - 
Letter a has to wait
name is now M
Letter r has to wait
M is notifying allLetter t has to wait
About to add i - 

Will shutdown thread pool in 20 seconds
Letter n has to wait
name is now Mi
i is notifying all
About to shutdown thread pool 
Letter a interrupted!
Letter t interrupted!
Letter n interrupted!
Letter r interrupted!
*/

As can be seen from the printed statements in the output, letters get blocked when they reach the busyAdding monitor and print the waiting message but they never come out of the waiting state even when a notifAlly() is sent.

Also, I would expect the interrupted threads to appear in the List returned by the shutdownNow() statement but the list is empty.

I have a feeling i am missing something obvious, can anyone spot what I am doing wrong here?

  • 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-14T16:55:44+00:00Added an answer on June 14, 2026 at 4:55 pm

    You are sharing the busyAdding between your different adders, but lock and notify on each different adder.

    This means that the following can happen. Say you create three adders, ‘O’, ‘p’ and ‘s’, the first might enter addLetterSynchWithNotifyAll, pass by the guard and set the busy signal to true. Now, the next two runners enter, and get stuck at the guard. When the first threads exist it notifies all threads waiting at the first runner (none at the moment). You want to share the signal between the objects.

    You can either make the addLetterSynchWithNotifyAll static which means the synchronised will be on class level, and then you need to change your wait to be on the class, not instance. Or you can create a common lock object used for synchronization and waiting.

    If you make the method static, notice that no other thread will be entering the method since they are now synchronized on the same object, and your guarding with busyAdding is unnecessary.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
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.