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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:00:13+00:00 2026-06-17T10:00:13+00:00

I have a bunch of threads running concurrently. Sometimes a thread needs to notify

  • 0

I have a bunch of threads running concurrently. Sometimes a thread needs to notify other threads to wait for it to finish a job and signal them again to resume. Since I’m somehow new to Java’s synchronization, I wonder what is the right way to do such thing. My code is something like this:

private void Concurrent() {
    if (shouldRun()) {
        // notify threads to pause and wait for them
        DoJob();
        // resume threads
    }

    // Normal job...
}

Update:

Note that the code I wrote is inside a class which will be executed by each thread. I don’t have access to those threads or how they are running. I’m just inside threads.

Update 2:

My code is from a crawler class. The crawler class (crawler4j) knows how to handle concurrency. The only thing I need is to pause other crawlers before running a function and resume them afterwards. This code is the basics of my crawler:

   public class TestCrawler extends WebCrawler {
    private SingleThread()
    {
        //When this function is running, no other crawler should do anything
    }

    @Override
    public void visit(Page page) {
        if(SomeCriteria())
        {
            //make all other crawlers stop until I finish
            SingleThread();
            //let them resume
        }

        //Normal Stuff
    }
   }
  • 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-17T10:00:15+00:00Added an answer on June 17, 2026 at 10:00 am

    Here is a short example on how to achieve this with the cool java concurrency stuff:

    snip old code doesn’t matter anymore with the Pause class.

    EDIT:

    Here is the new Test class:

    package de.hotware.test;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Test {
    
        private Pause mPause;
    
        public Test() {
            this.mPause = new Pause();
        }
    
        public void concurrent() throws InterruptedException {
            while(true) {
                this.mPause.probe();
                System.out.println("concurrent");
                Thread.sleep(100);
            }
        }
    
        public void crucial() throws InterruptedException {
            int i = 0;
            while (true) {
                if (i++ % 2 == 0) {
                    this.mPause.pause(true);
                    System.out.println("crucial: exclusive execution");
                    this.mPause.pause(false);
                } else {
                    System.out.println("crucial: normal execution");
                    Thread.sleep(1000);
                }
            }
        }
    
        public static void main(String[] args) {
            final Test test = new Test();
            Runnable run = new Runnable() {
    
                @Override
                public void run() {
                    try {
                        test.concurrent();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            };
            Runnable cruc = new Runnable() {
    
                @Override
                public void run() {
                    try {
                        test.crucial();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
            };
            ExecutorService serv = Executors.newCachedThreadPool();
            serv.execute(run);
            serv.execute(run);
            serv.execute(cruc);
        }
    
    }
    

    And the utility Pause class:

    package de.hotware.test;
    
    import java.util.concurrent.atomic.AtomicBoolean;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * Utility class to pause and unpause threads
     * with Java Concurrency
     * @author Martin Braun
     */
    public class Pause {
    
        private Lock mLock;
        private Condition mCondition;
        private AtomicBoolean mAwait;
    
        public Pause() {
            this.mLock = new ReentrantLock();
            this.mCondition = this.mLock.newCondition();
            this.mAwait = new AtomicBoolean(false);
        }
    
        /**
         * waits until the threads until this.mAwait is set to true
         * @throws InterruptedException
         */
        public void probe() throws InterruptedException {
            while(this.mAwait.get()) {
                this.mLock.lock();
                try {
                    this.mCondition.await();
                } finally {
                    this.mLock.unlock();
                }
            }
        }
    
        /**
         * pauses or unpauses
         */
        public void pause(boolean pValue) {
            if(!pValue){
                this.mLock.lock();
                try {
                    this.mCondition.signalAll();
                } finally {
                    this.mLock.unlock();
                }
            }
            this.mAwait.set(pValue);
        }
    
    }
    

    The basic usage is to call probe() before each run. This will block if it is paused until pause(false) is called.

    Your class would look like this:

    public class TestCrawler extends WebCrawler {
    
    private Pause mPause;
    
    public TestCrawler(Pause pPause) {
        this.mPause = pPause;
    }
    
    private SingleThread()
    {
            //When this function is running, no other crawler should do anything
    }
    
    @Override
    public void visit(Page page) {
        if(SomeCriteria())
        {
            //only enter the crucial part once if it has to be exclusive
            this.mPause.probe();
            //make all other crawlers stop until I finish
            this.mPause.pause(true);
            SingleThread();
            //let them resume
            this.mPause.pause(false);
        }
        this.mPause.probe();
        //Normal Stuff
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two threads, one needs to poll a bunch of separate static resources
I have a bunch of buttons that have a tapGestureRecognizer linked to them, and
I have a console application that starts up, hosts a bunch of services (long-running
I'm working a program that will have a bunch of threads processing data. Each
So basically the situation I am in is I have a bunch of threads
I have a program where a bunch of threads carry out some task. I
So I have a bunch of pthreads, where one is the main thread and
I have written a function which takes a whole bunch of files, zips them
I have a .NET application that is crashing sometimes on exit. There's a bunch
I have a bunch of threads, each creating an org.apache.qpid.client.AMQConnection and then a session.

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.