Results 1 to 6 of 6
  1. #1
    bibabene is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default ThreadPoolExecutor and many new threads during runtime

    Hey everybody,

    I am using the ThreadPoolExecutor to run a lot of threads depending on the processor count. Everything is working fine.

    But during runtime (when the threads are still running) I would like to generate new work and queue them so they can be executed when the older threads are done.

    So basically I would like to feed new work to the ThreadPoolExecutor when the pool runs low on work. So there are always 40-100 queued task and the threadpool executor is always busy.

    I would be so happy if anyone has an idea how to do that.

    Cheers to all,
    ben

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    When you construct your ThreadPoolExecutor, one of the arguments is a blocking work queue. That's how you should be feeding tasks to your Exectors. If you use, say, a LinkedBlockingQueue, then you can queue up an unbounded amoun of work. If you want to limit it to say 100 tasks, use an ArrayBlockingQueue.

  3. #3
    bibabene is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    yea thats what i found. but i have problems understanding how to use the linkedblockingqueue. do you have a link or example to thats stuff except form the documentation?

    Thanks for your reply :-)

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
    LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
    ...
    queue.put(myRunnable);

    The ThreadPoolExecutor will pull from the queue when work becomes available and there is a thread to work it (...assuming you've set it up for a fixed number of threads). If you're tasking it some other way, you're probably going to run into problems.

  5. #5
    bibabene is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Ok i got this. Thanks again :-)

    Last question in this context:
    How do i maintain the threads running so i can add new ones to the queue?

    Java Code:
    int tpSize = 2;
    int ticks = 0;
    int nTasks = 6;
    long n = 1000L;
    LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(100);
    
    ThreadPoolExecutor tpe = new ThreadPoolExecutor(tpSize, tpSize, 50000L, TimeUnit.MILLISECONDS, workQueue);
    
    Task[] tasks = new Task[nTasks];
            for (int i = 0; i < nTasks; i++) {
                tasks[i] = new Task(n, "Task " + i);
                tpe.execute(tasks[i]);
            }
    tpe.shutdown();
    This, for example can suddently end if the tasks are finished (or the for-loop finished) faster than I add them into the linkedblocking list (from another thread, which works like a charm).

    What can I do to keep the queue open all the time and waiting for me to add new work to it?

    Thanks in advance.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The queue doesn't shut down. If you put work into it, the ThreadPoolExecutor will pull the work off the queue in a maner consistent with how you set it up.

    Instead of calling 'execute' on tpe, add the tasks as work to the queue:

    Java Code:
    Task[] tasks = new Task[nTasks];
    
    for (int i = 0; i < nTasks; i++) {
      tasks[i] = new Task(n, "Task " + i);
      workQueue.put(tasks[i]);
    }

Similar Threads

  1. runtime error
    By ashaudhaya in forum New To Java
    Replies: 2
    Last Post: 11-12-2010, 10:13 AM
  2. Runtime.exec()
    By hknyo in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2008, 12:40 AM
  3. Example of RunTime class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-04-2008, 09:32 AM
  4. runtime method
    By roo7 in forum Advanced Java
    Replies: 5
    Last Post: 11-30-2007, 04:35 AM
  5. Nuxeo Runtime 1.3.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 08-10-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •