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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:33:04+00:00 2026-06-02T06:33:04+00:00

it seems to me that i get poor performance using the following SupervisedExecutor and

  • 0

it seems to me that i get poor performance using the following SupervisedExecutor and ExecutorSuperviser implementation of my own, What do you think can be unefficient in this code? I want to learn how can i improve its effciency.

ExecutorSuperviser Class:

public class ExecutorSuperviser {
private SupervisedExecutor[] threadPool;
private int poolSize = 0;
private LinkedList<Runnable> q;\\my own implementation of linkedlist
public ExecutorSuperviser(int nThreads) {
    threadPool=new SupervisedExecutor[poolSize=nThreads];
    q=new LinkedList<Runnable>();
    init();
}
public void execute(Runnable r) { 
    synchronized (q) {
        q.addToTail(r);
    }
    for (int i=0;i<poolSize;i++) 
            if (!threadPool[i].isBusy()) {
                if (!threadPool[i].isAlive()) threadPool[i].start();
                threadPool[i].interrupt();
                return;
            }


}
private void init() {
    for (int i=0;i<poolSize;i++) {
        threadPool[i]=new SupervisedExecutor(this);
    }

}
public Object getLock() {
    return q;
}
public Runnable getTask() {
    return q.removeHead();
}
public void terminate() {
    for (int i=0;i<poolSize;i++) 
        threadPool[i].terminate();
}
public void waitUntilFinished() {
    while (!isFinished()) {
        try {
            Thread.sleep(Thread.MAX_PRIORITY);
        } catch (InterruptedException e) {}
    }
}
private boolean isFinished() {
    for (int i=0;i<poolSize;i++) 
        if (threadPool[i].isBusy()) return false;
    return q.isEmpty();
}

}

SupervisedExecutor Class:

public class SupervisedExecutor extends Thread {
    private boolean   terminated = false;
    private Boolean busy = false;
    private ExecutorSuperviser boss;
    SupervisedExecutor (ExecutorSuperviser boss) {
        this.boss=boss;
    }
    public void run() {
        while (!terminated) {
            try {
                sleep(MAX_PRIORITY);
            } catch (InterruptedException e) {
                synchronized (busy) {
                    busy=true;
                }
                Runnable r;
                while (true) {
                    synchronized (boss.getLock()) {
                        r=boss.getTask();
                    }
                    if (r!=null) r.run();
                    else break;
                } 
                synchronized (busy) {
                    busy=false;
                }
            }
        }
    }

    public boolean isBusy() {
        boolean isBusy;
        synchronized (boss.getLock()) {
            isBusy=busy;
        }
        return isBusy;
    }
    public void terminate() {
        terminated=true;
    }

}
  • 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-02T06:33:05+00:00Added an answer on June 2, 2026 at 6:33 am

    How about the following solution which has the following advantages:

    1. As a subclass of ThreadPoolExecutor, you don’t have to re-implement everything which ThreadPoolExecutor does for you just to get the waitUntilFinished() functionality that you’re after.

    2. By taking advantage of ReentrantLock, Condition, and await()/signal() you avoid busy waiting, which can certainly hurt performance.

    This implementation works by taking advantage of the beforeExecute() and afterExecute() methods which ThreadPoolExecutor exposes to keep our own count of active tasks. I don’t use getActiveCount() because, according to the JavaDoc, it doesn’t guarantee an exact answer (although perhaps in the case of ThreadPoolExecutor it does provide an exact answer, I’d need to research further to be sure).

    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class WaitableThreadPoolExecutor extends ThreadPoolExecutor
    {
        private Condition waitCondition;
        private ReentrantLock lock;
        private int taskCount = 0;
    
        public WaitableThreadPoolExecutor( int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue )
        {
            super( corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue );
    
            lock = new ReentrantLock( );
            waitCondition = lock.newCondition( );
        }
    
        // if isEmpty() is true, then there is no need to block
        // otherwise, wait until waitCondition is signaled
        public void waitUntilFinished( )
        {
            lock.lock( );
            try
            {
                while ( !isEmpty( ) )
                    waitCondition.await( );
            }
            catch ( InterruptedException e )
            {
                e.printStackTrace();
            }
            finally
            {
                lock.unlock( );
            }
        }
    
        // the ThreadPool is empty if our taskCount is 0 and the
        // work queue is empty (this may not be bullet-proof, for one
        // thing, I'm hesitant to use getActiveCount() because it
        // does not guarantee an exact answer
        protected boolean isEmpty( )
        {
            lock.lock( );
            try
            {
                return taskCount == 0 && getQueue( ).isEmpty( );
            }
            finally
            {
                lock.unlock( );
            }
        }
    
        // increment our task count before executing each task
        @Override
        protected void beforeExecute( Thread t, Runnable r )
        {
            super.beforeExecute( t, r );
    
            lock.lock( );
            try
            {
                taskCount += 1;
            }
            finally
            {
                lock.unlock( );
            }
        }
    
        // decrement our task count after executing each task
        // then, if the pool is empty, signal anyone waiting
        // on the waitCondition
        @Override
        protected void afterExecute( Runnable r, Throwable t )
        {
            super.afterExecute( r, t );
    
            lock.lock( );
            try
            {
                taskCount -= 1;
    
                if ( isEmpty( ) ) waitCondition.signalAll( );
            }
            finally
            {
                lock.unlock( );
            }
        }
    
        public static void main( String[] args )
        {
            WaitableThreadPoolExecutor pool = new WaitableThreadPoolExecutor( 2, 4, 5000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>( ) );
    
            for ( int i = 0 ; i < 10 ; i++ )
            {
                final int threadId = i;
    
                pool.execute( new Runnable( )
                {
                    @Override
                    public void run( )
                    {
                        try { Thread.sleep( (int) ( Math.random( ) * 5000 ) ); } catch ( InterruptedException e ) { }
    
                        System.out.println( threadId + " done." );
                    }
                });
            }
    
            pool.waitUntilFinished( );
    
            System.out.println( "Done waiting." );
        }
    }
    

    I included a simple main() method which you can use as a test case. It starts 10 threads which wait a random amount of time before printing that they are done. Then the main thread calls waitUntilFinished().

    The results will look something like (the main point being that Done waiting. will always be printed last:

    1 done.
    2 done.
    0 done.
    4 done.
    3 done.
    5 done.
    7 done.
    8 done.
    6 done.
    9 done.
    Done waiting.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can get the LineHeight of a Font given the FontSize? It seems that
Using MongoDB C# driver ( http://github.com/samus/mongodb-csharp ), seems that I'm unable to get the
I've been investigating this issue that only seems to get worse the more I
It seems that I get the idea of call stack in programming language design.
i've created a video upload form and it seems that i get a limit
Map Reduce is a pattern that seems to get a lot of traction lately
I am trying to get my UniqueEmail validator working but it seems that my
Given that: There seems to be no easy way to get a list of
I never seem to get this right. I've got a method that returns a
I'm running into a problem that I can't seem to get past. Whenever I

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.