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 6957075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:57:09+00:00 2026-05-27T14:57:09+00:00

There is a singleton class that n threads can access. Each thread loads the

  • 0

There is a singleton class that n threads can access.
Each thread loads the instance of this class and invoke a method from this class in a loop.

I must control the flow of the execution so every thread can invoke the method one and pause, only after all thread invoked the method once the must resume their work.
Threads can call method in any order, just all of them have to execute method once before moving the loop.

This what I tried to do:

In thread:

 while ( some condition){
    ObjectType obj = theSingleton.getInstance().getSharedObject();
    obj.SomeMethod(threadID);
   if (obj.waitisneeded())
    synchronized (obj ) {
       obj.wait();
    }
}

what I did in SomeMethod:

public  synchronized void SomeMethod(String threadID) {
 hashMap.put(threadID,true);
 some job here
}

and in waitisneeded:

public  synchronized boolean waitisneeded(){
{

   Iterator iter = hashMap.entrySet().iterator();

   boolean alldone = false;

   while (iter.hasNext()) { 
      Map.Entry me = (Map.Entry) iter.next();
      String key = me.getKey().toString();
      alldone = (Boolean)me.getValue();

      if(!alldone)  {
    return false;
       }
   }

  //set all values to false 
 iter = hashMap.entrySet().iterator();
 while (iter.hasNext()) { 
   Map.Entry me = (Map.Entry) iter.next();
    String key = me.getKey().toString();
   me.setValue(false);      
 }      
 this.notifyAll();
 return true;               

Running this got me unexpected results and deadlocks.

How can I fix it?

Note: I can’t change how the thread is created, I can only change those methods!!! (plus the while loop in the thread where wait is)

  • 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-05-27T14:57:10+00:00Added an answer on May 27, 2026 at 2:57 pm

    If you know the number of threads, you can use a CyclicBarrier:

    static final CyclicBarrier barrier = new CyclicBarrier(numberOfThreads);
    

    Then, at the point in which you want to make all threads wait for the others, you call:

    barrier.await();
    

    If there’s a thread that has not yet arrived at that line, the thread calling await() will block. Once all the threads arrive there and call await(), they will all resume.

    Every time you must build some synchronization mechanism, be sure to check the package java.util.concurrent. There are lots of wonderful classes over there, created by experts. Most of the time you won’t need something custom, and if you do, there are some classes in the same package that will make it almost unecessary to use wait/notify directly — and, believe me, you want to avoid using these methods directly; as you can see, you can very easily get yourself into a deadlock!

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

Sidebar

Related Questions

How many number of Instance can be there when same singleton class is packaged
I'm looking to have a class (subclassed from threading.Thread) that is initialised and started
Suppose I have a Singleton class (any class can get the instance): class data
I know that singleton classes are inherently not thread-safe. But is there any way
Is there a way to implement a singleton object in C++ that is: Lazily
I have a singleton instance that is referenced throughout the project which works like
Is this thread-safe? The EventAggregator in Prism is a very simple class with only
I have a singleton class, whose instance get initialized at global scope within the
Using threads, I have a principal class ( SlaveCrawler ) that instantiates three classes(
In C#, the following code (from this page) can be used to lazily instantiate

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.