Thread pool executor problem

When using a thread pool executor (java.util.concurrent.ThreadPoolExecutor) to limit the number of threads executing at a time, the number of threads running still exceeds the limit number.
This is the code:
private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 20, TimeUnit.SECONDS, new LinkedBlockingQueue());
The number of tasks in my program are 4, so i always have 4 threads running, although i limited it to 3.
Can anyone help me with this problem? Or can u propose another solution to limit the number of running threads? By the way, i also tried using a newFixedThreadPool() and got the same problem.
Thx.

The number of tasks in my program are 4, so i always
have 4 threads running, although i limited it to 3.How do you know that there are 4 threads running? If you're generating a JVM thread dump, you're going to see threads that are used internally by the JVM.
Here's a simple program that creates a fixed-size threadpool and runs jobs. It limits the number of concurrent threads to 3. Compare it to what you're doing, I'm sure that you'll find something different. Also, verify that you're not creating threads somewhere else in your program; all of the ThreadPoolExecutor threads will have names of the form "pool-X-thread-Y"
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class ThreadPoolTest {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        for (int ii = 0 ; ii < 10 ; ii++) {
            pool.execute(new MyRunnable());
        pool.shutdown();
    private static class MyRunnable implements Runnable {
        public void run() {
            log("running");
            try {
                Thread.sleep(1000L);
            catch (InterruptedException e) {
                log("interrupted");
        private void log(String msg) {
            System.err.println(
                    msg + " on " + Thread.currentThread().getName()
                    + " at " + System.currentTimeMillis());
}

Similar Messages

  • Thread Pool executor

    I run a set of java programs as a single process in solaris os. Those java programs make use of thread pool executor to process threads and it runs in an infinite loop for ever. Thread processing is done whenever data is populated in database from an external source. The issue is after 2 to 3 days of running the process, it gets slowed down in executing the threads and finally it stops executing. What may be the reason for this.....how to solve this......... or how to fine tune thread pool executor in java or how to make best use of it...........

    Hey folks.... Sorry its a typo in my e-mail. Sorry about that. I am pasting the actual code here.
    The problem again is that in the index function only FirstRunnable executes but not the SecondRunnable
    final public class Crawler {
        / create an instance of the ISSThreadPoolExecutor /
        private static ThreadPoolExecutor mythreadpoolexecutor = ThreadPoolExecutor.getInstance();
        / Constructor /
        / Index function /
        public void index( .... ) {
            :::::: code :::::::::
            // Execute this folder in a seperate thread.
            this.issthreadpoolexecutor.execute(new FirstRunnable(alpha, beta, gamma));
        / The Inner Class /
        class FirstRunnable implements Runnable {
            public FirstRunnable(int alpha, int beta, int gamma) {
            public void run() {
                            doSomething();
                            // Some other tasks and ...spawn of another thread.
                            issthreadpoolexecutor.execute(new SecondRunnable(a));
             // The Inner Class that Indexes the Folder
              class SecondRunnable implements Runnable {
                      private int ei;
                      public SecondRunnable ( int abc ) {
                            this.ei = abc;
                      public void run() {
                            doSomething ( ".....") ;
              } // End of SecondRunnable Class.
         } // End of FirstRunnable class.
    } // End of Crawler Class.

  • Thread Pool , Executors ...

    Sorry if i make a stupid post now, but i'm looking for a implementation of a Thread Pool using the latest 1.5 java.util.concurrent classes and i can't find anything serious. Any implementation or link to a tutorial should be vary helpful.
    Thnx

    but i'm looking
    for a implementation of a Thread Pool using
    the latest 1.5 java.util.concurrent classes and i
    can't find anything serious. Any implementation or
    link to a tutorial should be vary helpful.
    Thnxhere is an Example :
    import java.util.concurrent.*;
    public class UtilConcurrentTest {
    public static void main(String[] args) throws InterruptedException {
    int numThreads = 4;
    int numTasks = 20;
    ExecutorService service = Executors.newFixedThreadPool(numThreads);
    // do some tasks:
    for (int i = 0; i < numTasks; i++) {
    service.execute(new Task(i));
    service.shutdown();
    log("called shutdown()");
    boolean isTerminated = service.awaitTermination(60, TimeUnit.SECONDS);
    log("service terminated: " + isTerminated);
    public static void log(String msg) {
    System.out.println(System.currentTimeMillis() + "\t" + msg);
    private static class Task implements Runnable {
    private final int id;
    public Task(int id) {
    this.id = id;
    public void run() {
    log("begin:\t" + this);
    try { Thread.sleep(1000); } catch (InterruptedException e) {}
    log("end\t" + this);
    public String toString() {
    return "Task " + id + " in thread " + Thread.currentThread().getName();
    }

  • Optimization of Thread Pool Executor

    Hi,
    I am using ThreadPoolexecutor by replacing it with legacy Thread.
    I have created executor as below:
    pool = new ThreadPoolExecutor(coreSize, size, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(coreSize), new CustomThreadFactory(name),new CustomRejectionExecutionHandler());
           pool.prestartAllCoreThreads();
    here core size is maxpoolsize/5
    and i have prestarted all the core threads on start up of application roughly around 160 threads.
    in legacy design we were creating and starting around 670 threads.
    But the point is even after using Executor and creating and replacing  legacy design we are not getting much better results.
    For results memory management we are using top command to see memory usage
    and for time we have placed loggers of System.currentTime in millis to check the usage.
    Please tell how to optimize this design
    Thanks & Regards,
    tushar

    The first step is to decide what you want to optimize. I'm guessing you want to minimize the total elapsed time for the job to run, but anyway that's your first step. Determine what you're optimizing. Then measure the performance of the job, with respect to that metric, for various pool sizes, and see what happens.
    However I'm not sure why you expected that rewriting the application to use the ThreadPoolExecutor would improve the runtime performance of the job. I would expect it might simplify the code and make it easier for future programmers to understand, but it's possible that your thread pool implementation performs just as well.

  • Thread Pool Executor ( Runnable Class Executing another Runnable Class )

    Hi Folks,
    I have my main class called ThreadPoolExecutorUser. I have two Runnable classes called XYZ and ABC
    in ThreadPoolExecutorUser class I execute the Runnable class XYZ. Which inturn executes Runnable class ABC.
    The problem is that the Runnable class ABC is never executed ?. Can some one please explain what I am I doing wrong.
    _RB
    More Description Below :
    *public class ThreadPoolExecutorUser {*
    ThreadPoolExecutor dude = new ThreadPoolExecutor (.... );
    // I Execute the firest Runnable Xyz here
    dude.execute ( XYZ );
    Now I have two Runnable inner Classes
    *Class XYZ extends Runnable {*
    public void run () {
    s.o.p ( " I am in Xyz Runnable " );
    dude.execute ( ABC );
    *class ABC extends Runnable {*
    public void run () {
    s.o.p ( " I am in ABC Runnable " );
    }

    Hey folks.... Sorry its a typo in my e-mail. Sorry about that. I am pasting the actual code here.
    The problem again is that in the index function only FirstRunnable executes but not the SecondRunnable
    final public class Crawler {
        / create an instance of the ISSThreadPoolExecutor /
        private static ThreadPoolExecutor mythreadpoolexecutor = ThreadPoolExecutor.getInstance();
        / Constructor /
        / Index function /
        public void index( .... ) {
            :::::: code :::::::::
            // Execute this folder in a seperate thread.
            this.issthreadpoolexecutor.execute(new FirstRunnable(alpha, beta, gamma));
        / The Inner Class /
        class FirstRunnable implements Runnable {
            public FirstRunnable(int alpha, int beta, int gamma) {
            public void run() {
                            doSomething();
                            // Some other tasks and ...spawn of another thread.
                            issthreadpoolexecutor.execute(new SecondRunnable(a));
             // The Inner Class that Indexes the Folder
              class SecondRunnable implements Runnable {
                      private int ei;
                      public SecondRunnable ( int abc ) {
                            this.ei = abc;
                      public void run() {
                            doSomething ( ".....") ;
              } // End of SecondRunnable Class.
         } // End of FirstRunnable class.
    } // End of Crawler Class.

  • Multiple thread pools under single executor

    I would like to segregate the tasks running in an Executor so that certain tasks (perhaps identified by an annotation) could be restricted to a thread pool of a limited size while others run in a different pool.
    The intend is to limit certain crunchy tasks so that they don't saturate a limited resource.
    As far as I can tell I can't do this with a ThreadPoolExecutor even with a custom ThreadFactory. The original Callable is wrapped a couple of times by the time the ThreadFactory.newThread method is called so I can't inspect it at that time. Also, once the maximumPoolSize is reached the waiting tasks will get handed off to whichever thread finishes first, thus making my fixed pools useless.
    I have a feeling that I will need to write my own ThreadPoolExecutor but I thought that I would ask if there was a way to do this with the existing classes first.
    Thanks.

    I used to do this.
    The problem comes in managing the work that flows between the queues, and managing the results. I need to be able to take the output from one task and feed it into another, potentially running in a different thread pool.
    Multiple queues mean waiting for more than one blocking event, which means multiple management threads with a meta-event queue feeding back to an dispatcher that does the messaging.
    With multiple management threads you have to poison all of those queues so that the thing shuts down when you are done.
    By the time that I'm done I may be better off just modifyingThreadPoolExecutor to do what I want. One work queue keeps the shutdown problem under control.
    Thanks for the feedback.

  • The problem in the thread pool implemented by myself

    Hello, I need to a thread pool in J2ME CDC 1.0 + FP 1.0, so I implemented a simple one by myself that also meets my own requirement.
    Here is the main idea:
    The thread pool creates a fixed number of threads in advance. When a task comes, it is put in the waiting list. All threads tries to get the tasks from the waiting list. If no task exists, the threads wait until someone wakes them up.
    Here are the requirements from myself:
    1. when a task has finished its work in one execution, it is put in the waiting list for the next run.
    2. the task can control the delay between when the task owner tries to put it in the waiting list and when the task is actually put in the waiting list. I need this function because sometimes I don't want the tasks to run too often and want to save some CPU usage.
    In my program, I creates two thread pools. In one pool, every task don't use the delay, and the thread pool works very well. The other pool has the tasks that use the delay, and sometimes, as I can see from the printed information, there are many tasks in the waiting list but 0 or 1 thread executes tasks. It seems that the waiting threads cannot wake up when new tasks comes.
    I suspect the code in addTask(), but cannot find the reason why it fails. Could anyone please help me find out the bug in my code? I put the code of thread pool below
    Thank you in advance
    Zheng Da
    ThreadPool.java
    package j2me.concurrent;
    import java.util.LinkedList;
    import java.util.Timer;
    import java.util.TimerTask;
    import alvis.general.Util;
    public class ThreadPool {
         private int maxQueueSize;
         private boolean running = true;
         private Thread[] threads;
         private LinkedList tasks = new LinkedList();
         private Timer timer = new Timer(true);
         private AtomicInteger usingThreads = new AtomicInteger(0);
         private synchronized boolean isRunning() {
              return running;
         private synchronized void stopRunning() {
              running = false;
         private synchronized PoolTask getTask() {
              while (tasks.isEmpty() && isRunning()) {
                   try {
                        this.wait();
                   } catch (InterruptedException e) {
                        e.printStackTrace();
              if (tasks.isEmpty())
                   return null;
              // Util.log.info(Thread.currentThread().getName() +
              // " gets a task, left tasks: " + tasks.size());
              return (PoolTask) tasks.removeFirst();
         private synchronized void addTaskNoDelay(PoolTask task) {
              tasks.addLast(task);
              notifyAll();
         private synchronized void addTask(final PoolTask task) {
              long delay = task.delay();
              if (delay == 0) {
                   addTaskNoDelay(task);
              } else {
                   timer.schedule(new TimerTask() {
                        public void run() {
                             addTaskNoDelay(task);
                   }, delay);
         private synchronized int numTasks() {
              return tasks.size();
         private class PoolThread extends Thread {
              public void run() {
                   Util.poolThreads.inc();
                   while (isRunning()) {
                        PoolTask task = getTask();
                        if (task == null) {
                             Util.poolThreads.dec();
                             return;
                        usingThreads.inc();
                        long currentTime = System.currentTimeMillis();
                        task.run();
                        long elapsedTime = System.currentTimeMillis() - currentTime;
                        if (elapsedTime > 100)
                             System.err.println(task.toString() + " takes " + ((double) elapsedTime)/1000 + "s");
                        usingThreads.dec();
                        if (!task.finish()) {
                             addTask(task);
                   Util.poolThreads.dec();
         public ThreadPool(int size, int taskQueueSize) {
              maxQueueSize = taskQueueSize;
              threads = new Thread[size];
              for (int i = 0; i < threads.length; i++) {
                   threads[i] = new PoolThread();
                   threads.start();
         public synchronized boolean executor(PoolTask task) {
              if (!isRunning()) {
                   return false;
              Util.log.info("Thread Pool gets " + task + ", there are "
                        + numTasks() + " waiting tasks");
              if (numTasks() >= maxQueueSize) {
                   return false;
              addTask(task);
              return true;
         public synchronized void destroy() {
              stopRunning();
              timer.cancel();
              // TODO: I am not sure it can wake up all threads and destroy them.
              this.notifyAll();
         public synchronized void printSnapshot() {
              System.err.println("using threads: " + usingThreads + ", remaining tasks: " + tasks.size());
    PoolTask.javapackage j2me.concurrent;
    public interface PoolTask extends Runnable {
         * It shows if the task has already finished.
         * If it isn't, the task will be put in the thread pool for the next execution.
         * @return
         boolean finish();
         * It shows the delay in milliseconds that the task is put in the thread pool.
         * @return
         long delay();

    are receiving/sends tasks packets time consuming operation in your case or not? if it is not you do not need to use thread pools at all. you can create a queue like in your code through the linked list and dispatch this queue periodically with minimum monitor usage. try this.
    import java.util.LinkedList;
    public class PacketDispatcher extends Thread {
        LinkedList list = new LinkedList();
        public PacketDispatcher (String name) {
            super(name);
        public void putTask(Task task) {
            synchronized (list) {
                list
                        .add(task);
                list.notify();
        public void run() {
            while (true/* your condition */) {
                Task task = null;
                synchronized (list) {
                    while (list.isEmpty())
                        try {
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                    task = (Task)list
                            .poll();
                if (task == null) {
                    try {
                        Thread
                                .sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    continue;
                task
                        .run();
                if (!task.isFinished()) {
                    putTask(task);
                Thread
                        .yield();
        public static void main(String[] args) {
            // just for test
            try {
                Thread.sleep (10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            PacketDispatcher dispatcher = new PacketDispatcher("Packet Dispatcher");
            Task task = new Task();
            dispatcher.putTask(task);
            dispatcher.start();
            try {
                Thread.sleep (10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            Task task2 = new Task();
            dispatcher.putTask(task2);
    class Task {
        long result = 0;
        public boolean isFinished () {
            if (getResult() >= 10000000) {
                return true;
            return false;
        public void run() {
            for (int i = 0; i < 1000; i++) {
                result += i;
        public long getResult () {
            return result;       
    }

  • Thread Pool Problem

    Hi,
    I amhaving problem with a thread pooling my code once allocates a thread to a user then never is able to find that it is idle. the code is some what like this.
    public class foo extends Thread{
    pubblic void setPrimeData(Hashtable data){
    //sets the primary data
    run()
    this.isrunning = true;
    //does something
    this.isrunning= false;
    }

    You cannot restart a Thread. Once it has run, it cannot be reused. If you want to hand new tasks to threads in a pool, device some means by which you can hand a running (started) thread a new Runnable to execute in its run() method.
    Chuck

  • Aplication server thread pool problem

    I'm using sun app server 8.
    After some time from starting (and using) the server, it stops responding to clients.
    When I change the max number of threads on server the number of clients it can serve before hanging folows the change. So I guess that some threads are not recycled.
    But, I can't get full thread dump to see what's happening.
    Also I can't get any thread pool monitoring information through asadmin.
    (I can see that EJB's are all removed successfuly)
    Any suggestions.
    Thanks in advance.

    First of all, thank you for helping me.
    The client wasn't making problems, but server did. (I didn't said that I use the app. server on XP.)
    For now I solved the problem by installing the new beta 2004Q4. It works fine now, it also has some thread monitoring in web console...
    I was getting this, when I tried to monitor the thread-pool (it is set on HIGH):
    asadmin> get -m server.thread-pools.thread-pool.thread-pool-1.*
    No matches resulted from the wildcard expression.
    CLI137 Command get failed.
    If it means anything this is what I was getting when I do ctrl-break. (this thread dump stays the same even after server stops responding...)
    Full thread dump Java HotSpot(TM) Client VM (1.4.2_04-b04 mixed mode):
    "Thread-6" prio=5 tid=0x02edad08 nid=0xb40 runnable [331f000..331fd8c]
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:177)
    at org.apache.commons.launcher.StreamConnector.run(StreamConnector.java:
    115)
    "Thread-5" prio=5 tid=0x02ebbb98 nid=0x8ac runnable [32df000..32dfd8c]
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:194)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:220)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:277)
    - locked <0x10089900> (a java.io.BufferedInputStream)
    at java.io.FilterInputStream.read(FilterInputStream.java:90)
    at org.apache.commons.launcher.StreamConnector.run(StreamConnector.java:
    115)
    "Signal Dispatcher" daemon prio=10 tid=0x0093dc18 nid=0x930 waiting on condition
    [0..0]
    "Finalizer" daemon prio=9 tid=0x008a5c20 nid=0xbd0 in Object.wait() [2b5f000..2b
    5fd8c]
    at java.lang.Object.wait(Native Method)
    - waiting on <0x10502a00> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:111)
    - locked <0x10502a00> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:127)
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159)
    "Reference Handler" daemon prio=10 tid=0x008a47f0 nid=0xb4 in Object.wait() [2b1
    f000..2b1fd8c]
    at java.lang.Object.wait(Native Method)
    - waiting on <0x10502a68> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:429)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:115)
    - locked <0x10502a68> (a java.lang.ref.Reference$Lock)
    "main" prio=5 tid=0x000362a0 nid=0xc38 runnable [7f000..7fc3c]
    at java.lang.Win32Process.waitFor(Native Method)
    at org.apache.commons.launcher.LaunchTask.execute(LaunchTask.java:705)
    at org.apache.tools.ant.Task.perform(Task.java:341)
    at org.apache.tools.ant.Target.execute(Target.java:309)
    at org.apache.tools.ant.Target.performTasks(Target.java:336)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1339)
    at org.apache.commons.launcher.Launcher.start(Launcher.java:402)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at LauncherBootstrap.main(LauncherBootstrap.java:185)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at com.sun.enterprise.admin.servermgmt.pe.PEInstancesManager.startInstan
    ce(PEInstancesManager.java:115)
    at com.sun.enterprise.admin.servermgmt.pe.PEDomainsManager.startDomain(P
    EDomainsManager.java:126)
    at com.sun.enterprise.cli.commands.StartDomainCommand.runCommand(StartDo
    mainCommand.java:59)
    at com.sun.enterprise.cli.framework.CLIMain.invokeCommand(CLIMain.java:1
    23)
    at com.sun.enterprise.cli.framework.CLIMain.main(CLIMain.java:39)
    "VM Thread" prio=5 tid=0x0093c698 nid=0x9b4 runnable
    "VM Periodic Task Thread" prio=10 tid=0x00940438 nid=0xbd4 waiting on condition
    "Suspend Checker Thread" prio=10 tid=0x0093d2b8 nid=0x2c0 runnable

  • Thread pools problem

    How do we know that a given task(implementing Callable) in a thread pool (ThreadPoolExecutor) is completed ?
    I know that we can use the get( ) method of Future class... but for that the current thread has to wait until the Task given to the Thread pool completed.
    But I want to know ..Is it possible to get (asynchronus)notification from Thread pool when one of the submitted task finish.

    What about using the isDone() method of Future or even get(timeout, TimeUnit.SECONDS).
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html

  • Creating Thread Pool with Executors.newFixedThreadPool(poolSize)

    I am creating a server socket and want a Thread pool to limit the number of threads created for serving each clients. I have written a code:
    private ExecutorService threadPool;
    //Creating a Thread Pool for handling new sessions
    threadPool=ThreadPool.createSessionThreadPool(poolSize);
    and in my main listen logic:
    serverSocket = new ServerSocket(listenPort);
    clientSocket = serverSocket.accept();
                        threadPool.execute(new Runnable(){
                             public void run(){
                                  handleConnection();
    and in handleConnection I am trying to read the inputstream in a while loop:
    public void handleConnection(){
    BufferedReader in=null;
    in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    while (true) {
    if((fullCommandString = in.readLine()) ==null){
    wait(); // I assume that this wait will block the thread and keep the thread back in to the pool.
                        System.out.println(Thread.currentThread().getName() + " received the message");
                        System.out.println("echo: " + fullCommandString);
                        out.println("230 reply back \r\n");     
    why don't it keep the thread back in the pool when I call the wait() ? Is it since the thread is still active in reading the input stream? If so can any one hepl me with a snippet which will keep the thread back in the pool when there is noting on the input stream to read.

    Hi,
    The call to wait does not return the thread pool back to the pool. The thread will be associated with your runnable as long as your run method hasn't returned.
    It looks like you instead want to use NIO if you want to use a few threads to handle many clients.
    Kaj

  • The problem of thread pool

    I want to make a thread pool to handle UDP package request
    so I set tow int variable :
    norsize;
    maxsize;
    the norsize is normal amount threads when Server boot they will be creat;
    the maxsize is when the quest is large and the norsize threads are not enough,The
    Thead pool can increase to the amount
    but how reduce the Thread pool amount when it reach the maxsize????

    That was funy (the Duke Dollars part) :)
    Ok. This time with some code.
    We have a class that manages work. This work is made up of Runnable objects and the threads that run it. Let's go with sample code instead of english, just remenber that I wrote this code directly to the post text area and didn't event compile it, although it should work.
    publi class WorkQueue {
        private LinkedList m_listRunnable;
        private int m_nMinThread;
        private int m_nMaxThread;
        private int m_nTimeOut;
        private int m_nNumThread;
        private int m_nNumThreadWaiting;
        public WorkQueue(int minThread, int maxThread, int timeOut) {
            m_listRunnable = new LinkedList();
            m_nMinThread = minThread;
            m_nMaxThread = maxThread;
            m_nTimeOut = timeOut;
            m_nNumThread = 0;
            m_nNumThreadWaiting = 0;
            init();
        private void init() {
            /* Start MinThread threads */
            for (i = 0; i < m_nMinThread; i++) {
                 new WorkSlaveThread();
                 m_nNumThread++;
        /* Inner class for the work thread */
        private class WorkSlaveThread extends Thread {
            public WorkSlaveThread() {
            public void run() {
                /* Work unless no work is returned, then it should stop */
                for (Runnable work = getNextWork(); work != null; work = getNextWork()) {
                    try {
                        work.run();
                    } catch (Throwable ex) {
                        /* Log the error */
                        ex.printStackTrace();
                threadStopping();
        private void threadStopping() {
            synchronized (this) {
                m_nNumThread--;
        private Runnable getNextWork() {
            Runnable work;
            long starttime = System.currentTimeMillis();
            long currenttime = starttime;
            long timeOut = m_nTimeOut * 1000; // Assume the time on in the constructor is in seconds
            synchronized (m_listRunnable) {
                /* Until there is work and the time out hasn't exceed */
                /* (time out ignored if the minimum umber of threads is reached) */
                while ((m_listRunnable.size() == 0) && ((timeOut > (currenttime - starttime)) || (m_nThreads == m_nMinThreads))) {
                    try {
                        m_nNumThreadWaiting++;
                        m_listRunnable.wait(m_nTimeOut * 1000);
                    } catch (Exception ex) {
                        /* Exception are launched if the VM is going down */
                        return null;
                    } finally {
                        m_nNumThreadWaiting--;
                    currenttime = System.currentTimeMillis();
                if (m_listRunnable.size () == 0) {
                    /* Stop threads procedure */
                    return null; // Null will stop the thread
                } else {
                    work = (Runnable)m_listRunnable.remove(0);
                    return work;
        public void addWork(Runnable work) {
            synchronized (m_listRunnable) {
                m_listRunnable.add(work);
                /* Start threads if necessary */
                if ((m_nNumThreadWaiting == 0) && (m_nNumThread < m_nMaxthread)) {
                    new WorkSlaveThread();
                    synchronized (this) {
                        m_nNumThread++;
                /* Signal a waiting thread */
                m_listRunnable.notify();
    }Well, this about does it! I didn't compile this, nor this is the code I use internally (internally I have shutdown procedures to ensure all work is down and other things), but it should do all the things a mention previously (I don't think I forgot any thing).
    Hope this helps,
    Nuno

  • A good design for a single thread pool manager using java.util.concurrent

    Hi,
    I am developing a client side project which in distinct subparts will execute some tasks in parallel.
    So, just to be logorroic, something like that:
    program\
                \--flow A\
                           \task A1
                           \task A2
                \--flow B\
                            \task B1
                            \task B2
                            \...I would like both flow A and flow B (and all their launched sub tasks) to be executed by the same thread pool, because I want to set a fixed amount of threads that my program can globally run.
    My idea would be something like:
    public class ThreadPoolManager {
        private static ExecutorService executor;
        private static final Object classLock = ThreadPoolManager.class;
         * Returns the single instance of the ExecutorService by means of
         * lazy-initialization
         * @return the single instance of ThreadPoolManager
        public static ExecutorService getExecutorService() {
            synchronized (classLock) {
                if (executor != null) {
                    return executor;
                } else {
                    // TODO: put the dimension of the FixedThreadPool in a property
                    executor = Executors.newFixedThreadPool(50);
                return executor;
         * Private constructor: deny creating a new object
        private ThreadPoolManager() {
    }The tasks I have to execute will be of type Callable, since I expect some results, so you see an ExecutorService interface above.
    The flaws with this design is that I don't prevent the use (for example) of executor.shutdownNow(), which would cause problems.
    The alternative solution I have in mind would be something like having ThreadPoolManager to be a Singleton which implements ExecutorService, implementing all the methods with Delegation to an ExecutorService object created when the ThreadPoolManager object is instantiated for the first time and returned to client:
    public class ThreadPoolManager implements ExecutorService {
        private static ThreadPoolManager pool;
        private static final Object classLock = ThreadPoolManager.class;
        private ExecutorService executor;
         * Returns the single instance of the ThreadPoolManager by means of
         * lazy-initialization
         * @return the single instance of ThreadPoolManager
        public static ExecutorService getThreadPoolManager() {
            synchronized (classLock) {
                if (pool !=null) {
                    return pool;
                } else {
                    // create the real thread pool
                    // TODO: put the dimension of the FixedThreadPool in a property
                    // file
                    pool = new ThreadPoolManager();
                    pool.executor = Executors.newFixedThreadPool(50);
                    // executor = Executors.newCachedThreadPool();
                    return pool;
         * Private constructor: deny creating a new object
        private ThreadPoolManager() {
        /* ======================================== */
        /* implement ExecutorService interface methods via delegation to executor
         * (forbidden method calls, like shutdownNow() , will be "ignored")
          // .....I hope to have expressed all the things, and hope to receive an answer that clarifies my doubts or gives me an hint for an alternative solution or an already made solution.
    ciao
    Alessio

    Two things. Firstly, it's better to use     private static final Object classLock = new Object();because that saves you worrying about whether any other code synchronises on it. Secondly, if you do decide to go for the delegation route then java.lang.reflect.Proxy may be a good way forward.

  • Fixed Size Thread Pool which infinitely serve task submitted to it

    Hi,
    I want to create a fixed size thread pool say of size 100 and i will submit around 200 task to it.
    Now i want it to serve them infinitely i.e once all tasks are completed re-do them again and again.
    public void start(Vector<String> addresses)
          //Create a Runnable object of each address in "addresses"
           Vector<FindAgentRunnable> runnables = new Vector<FindAgentRunnable>(1,1);
            for (String address : addresses)
                runnables.addElement(new FindAgentRunnable(address));
           //Create a thread pool of size 100
            ExecutorService pool = Executors.newFixedThreadPool(100);
            //Here i added all the runnables to the thread pool
             for(FindAgentRunnable runnable : runnables)
                    pool.submit(runnable);
                pool.shutdown();
    }Now i wants that this thread pool execute the task infinitely i.e once all the tasks are done then restart all the tasks again.
    I have also tried to add then again and again but it throws a java.util.concurrent.RejectedExecutionException
    public void start(Vector<String> addresses)
          //Create a Runnable object of each address in "addresses"
           Vector<FindAgentRunnable> runnables = new Vector<FindAgentRunnable>(1,1);
            for (String address : addresses)
                runnables.addElement(new FindAgentRunnable(address));
           //Create a thread pool of size 100
            ExecutorService pool = Executors.newFixedThreadPool(100);
            for(;;)
                for(FindAgentRunnable runnable : runnables)
                    pool.submit(runnable);
                pool.shutdown();
                try
                    pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
                catch (InterruptedException ex)
                    Logger.getLogger(AgentFinder.class.getName()).log(Level.SEVERE, null, ex);
    }Can anybody help me to solve this problem?
    Thnx in advance.

    Ravi_Gupta wrote:
    *@ kajbj*
    so what should i do?
    can you suggest me a solution?Consider this thread "closed". Continue to post in your other thread. I, and all others don't want to give answers that already have been given.

  • Submit submit a large number of task to a thread pool (more than 10,000)

    i want to submit a large number of task to a thread pool (more than 10,000).
    Since a thread pool take runnable as input i have to create as many objects of Runnable as the number of task, but since the number of task is very large it causes the memory overflow and my application crashes.
    Can you suggest me some way to overcome this problem?

    Ravi_Gupta wrote:
    I have to serve them infinitely depending upon the choice of the user.
    Take a look at my code (code of MyCustomRunnable is already posted)
    public void start(Vector<String> addresses)
    searching = true;What is this for? Is it a kind of comment?
    >
    Vector<MyCustomRunnable> runnables = new Vector<MyCustomRunnable>(1,1);
    for (String address : addresses)
    try
    runnables.addElement(new MyCustomRunnable(address));
    catch (IOException ex)
    ex.printStackTrace();
    }Why does MyCustomRunnable throw an IOException? Why is using up resources when it hasn't started. Why build this vector at all?
    >
    //ThreadPoolExecutor pool = new ThreadPoolExecutor(100,100,50000L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue());
    ExecutorService pool = Executors.newFixedThreadPool(100);You have 100 CPUs wow! I can only assume your operations are blocking on a Socket connection most of the time.
    >
    boolean interrupted = false;
    Vector<Future<String>> futures = new Vector<Future<String>>(1,1);You don't save much by reusing your vector here.
    for(int i=1; !interrupted; i++)You are looping here until the thread is interrupted, why are you doing this? Are you trying to generate loading on a remote server?
    System.out.println("Cycle: " + i);
    for(MyCustomRunnable runnable : runnables)Change the name of you Runnable as it clearly does much more than that. Typically a Runnable is executed once and does not create resources in its constructor nor have a cleanup method.
    futures.addElement((Future<String>) pool.submit(runnable));Again, it unclear why you would use a vector rather than a list here.
    >
    for(Future<String> future : futures)
    try
    future.get();
    catch (InterruptedException ex)
    interrupted = true;If you want this to break the loop put the try/catch outside the loop.
    ex.printStackTrace();
    catch (ExecutionException ex)
    ex.printStackTrace();If you are generating a load test you may want to record this kind of failure. e.g. count them.
    futures.clear();
    try
    Thread.sleep(60000);Why do you sleep even if you have been interrupted? For better timing, you should sleep, before check if you futures have finished.
    catch(InterruptedException e)
    searching = false;again does nothing.
    System.out.println("Thread pool terminated..................");
    //return;remove this comment. its dangerous.
    break;why do you have two way of breaking the loop. why not interrupted = true here.
    searching = false;
    System.out.println("Shut downing pool");
    pool.shutdownNow();
    try
    for(MyCustomRunnable runnable : runnables)
    runnable.close(); //release resources associated with it.
    catch(IOException e)put the try/catch inside the loop. You may want to ignore the exception but if one fails, the rest of the resources won't get cleaned up.
    The above code serve the task infinitely untill it is terminated by user.
    i had created a large number of runnables and future objects and they remain in memory until
    user terminates the operation might be the cause of the memory overflow.It could be the size of the resources each runnable holds. Have you tried increasing your maximum memory? e.g. -Xmx512m

Maybe you are looking for

  • Enhancement Fields missing in the Inbound XML payload

    We are using XI3.0. We need to send some Purchase Order data from R/3 to SRM(SUS), in addition to what is available in the standard content. We have created the required datatype enhnacement. we have mapped these additional fields in the message mapp

  • Apple Reuse and Recycling Program

    I'm not sure where this should live. Its not a question more of a warning. I recently took advantage of the Apple Reuse and Recycle Program in the UK to recycle three iPhones and obtain some vouchers in return. The Apple Partner for this service in t

  • Creative Vision M Probl

    The other day the screen on my creative vision M froze so I hit the reset button on the bottom and continued to charge it by USB at my computer. However my mom used it the next day and told me that it seemed to not have charged at all so I decided to

  • Is it safe yet?

    Hi, I have Tiger on my iMac and want to upgrade to Leopard but am concerned about all the problems being posted here. I am particularly interesting in Time Machine but hear a lot of problems reported there too. I would imagine that a fresh installati

  • How to join to a column contains trailing spaces

    I am trying to do a Left Outer Join between the above 2 tables. select a.name, a.address, a.city, a.state, a.zip, nvl(b.zip_id,0) as zip_id from t1 a, t2 b where a.zip = b.zip(+) The problem is that t2.zip is of 9 character size values with 5 digit z