Timeouts and Executors...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Timeouts and Executors...

ttabbal
I have an application that has a limited number of outside connections
available. So I need to limit the pool size to prevent overloading the
remote server. I also want to be able to queue new tasks for a short time
to see if a thread becomes available. Is there anything in
java.util.concurrent to do something like this, or am I going to need to
write my own Executor?

So something like this:

if (threadsAreAvailable)
  runTask()
else
  if (queue.offer(task, timeout, units))
  {
    // It's queued so it can run later
  }
  else
  {
    // Throw an exception or return an error code
  }


Thanks,

Travis Tabbal


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: Timeouts and Executors...

Peter Veentjer - Anchor Men
the threadpoolexecutor rejects commands (it throws a
RejectedExecutionException) if the queue is full and the the threadpool
is filled  to the maximum. So it doesn`t block untill there is space. If
you deal with the RejectedExecutionException, you could retry later. You
also can inject a custom verion of the RejectedExecutionHandler.

In some cases I need more control and that is why I have created the
BlockingExecutor (extends the Executor) and the
ThreadPoolBlockingExecutor (extends the ThreadPoolExecutor) that adds
timeout behaviour to the Executor. If you like I can send you the
sources.

this is the main interface of the BlockingExecutor:

/**
 * Copyright 2005 Peter Veentjer.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jph.concurrent.blockingExecutors;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executor;

/**
 * The BlockingExecutor is an Exector that gives more control on
blocking and timeout
 * behaviour.
 *
 * @author Peter Veentjer.
 */
public interface BlockingExecutor extends Executor{

        /**
         * Offers the given command to be executed at some time in the
future. This
         * call will block until:
         * <ul>
         * <li>the command is offered succesfully. The
command will be executed.</li>
         *      <li>a InterruptedException is thrown. The command won`t
be executed.</li>
         * <ul>
         *
         * Difference between execute and put:
         * <ul>
         * <li><b>execute</b> can reject a command while
the executor is
         * running. The ThreadPoolExecutor will
reject a command if the
         * queue is full and the threadpool is
full.
         * </li>
         * <li><b>put</b> can`t reject a command while the
executor is running but
         * blocks untill the command can be
executed (at some time in the future).
         * </li>
         * </ul>
         *
         * @param command the command to execute.
         * @throws InterruptedException if the current thread has been
interrupted. If that happens,
         *                              the command won`t be executed.
         * @throws NullPointerException if command is null.
         */
        void put(Runnable command) throws InterruptedException;

        /**
         * Offers the given command to be executed at some time in the
future. This
         * call will block until:
         * <ul>
         * <li>the command is offered succesful. The
command will be executed
         * at some time in the future.
         * </li>
         * <li>a timeout occured. The command won`t be
executed.</li>
         * <li>a InterruptedException is thrown. The
command won`t be executed.</li>
         * </ul>
         *
         * @param command the command to execute.
         * @param timeout how long to wait before giving up.
         * @param unit    the time unit of the timeout argument
         * @return true if successful, or false if the specified
waiting time elapses
         * before space is available.
         * @throws InterruptedException if the current thread has been
interrupted. If that happens,
         *                              the command won`t be executed.
         * @throws NullPointerException if command or unit is null.
         */
        boolean offer(Runnable command, long timeout, TimeUnit unit)
throws InterruptedException;
}



-----Oorspronkelijk bericht-----
Van: [hidden email]
[mailto:[hidden email]] Namens Travis Tabbal
Verzonden: woensdag 7 december 2005 15:58
Aan: [hidden email]
Onderwerp: [concurrency-interest] Timeouts and Executors...

I have an application that has a limited number of outside connections
available. So I need to limit the pool size to prevent overloading the
remote server. I also want to be able to queue new tasks for a short
time to see if a thread becomes available. Is there anything in
java.util.concurrent to do something like this, or am I going to need to
write my own Executor?

So something like this:

if (threadsAreAvailable)
  runTask()
else
  if (queue.offer(task, timeout, units))
  {
    // It's queued so it can run later
  }
  else
  {
    // Throw an exception or return an error code
  }


Thanks,

Travis Tabbal


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: Timeouts and Executors...

Peter Veentjer - Anchor Men
In reply to this post by ttabbal
I have added the sources and the jar (you can find it in the target
directory).

The project isn`t finished. Some work need to be done with the Repeaters
and the CapacityQueues. But I have used this project the last few months
in a few projects and it works. And it is part of
http://channels.sourceforge.net (another project of mine I`m currently
working on).

-----Oorspronkelijk bericht-----
Van: Travis Tabbal [mailto:[hidden email]]
Verzonden: woensdag 7 december 2005 16:52
Aan: Peter Veentjer - Anchor Men
Onderwerp: RE: [concurrency-interest] Timeouts and Executors...

Those classes sound interesting. I would love to go over the sources.
Thanks for the help!

Travis


> the threadpoolexecutor rejects commands (it throws a
> RejectedExecutionException) if the queue is full and the the
> threadpool is filled  to the maximum. So it doesn`t block untill there

> is space. If you deal with the RejectedExecutionException, you could
> retry later. You also can inject a custom verion of the
RejectedExecutionHandler.
>
> In some cases I need more control and that is why I have created the
> BlockingExecutor (extends the Executor) and the
> ThreadPoolBlockingExecutor (extends the ThreadPoolExecutor) that adds
> timeout behaviour to the Executor. If you like I can send you the
> sources.



concurrent.zip (60K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Timeouts and Executors...

Holger Hoffstätte-2
In reply to this post by ttabbal
Travis Tabbal wrote:
> I have an application that has a limited number of outside connections
> available. So I need to limit the pool size to prevent overloading the
> remote server. I also want to be able to queue new tasks for a short time
> to see if a thread becomes available. Is there anything in
> java.util.concurrent to do something like this, or am I going to need to
> write my own Executor?

You might just need a custom policy, not an entirely new executor (at
least if I understood you requirement correctly). Since dl.util.concurrent
contained such a policy I had to retrofit it for a backport project.
You can find my WaitPolicy here (look on the right side where the package
classes are listed):
http://cvs.mule.codehaus.org/viewrep/mule/mule/mule/src/java/org/mule/util/concurrent

I wrote this and a corresponding unit test (same repo but in
*/mule/test/*) against native util.concurrent and then backported both; it
seems to work exactly as we required.

hope this helps,
Holger
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest