Thread pool + multiple work queues

Hi,
I guess the problem I am facing is pretty common but I don't know what's the best pattern for this.
I have multiple queues. Queue element is "Runnable". The requirement is that "Runnable"s inside the same queue must be run serializely. I want to use a ThreadPool to do this since I want concurrency (different queues can run simultanously) but don't want too many threads.
I know Java 5 Concurrent API (newbie though). But for this problem, I don' t know what classes I should combine. Can anyone give an example or maybe a link to a good article?
Thank you!

java.util.concurrent.Executors
see newSingleThreadExecutor()
submit() you Runnable to be executed. submit() will return a Future.
Future.get() will block waiting for the Runnable you submitted to complete its execution before continuing.
When get() returns, submit() the next Runnable.

Similar Messages

  • Thread pools for execute queues

    We've set up thread pools for several execute queues dedicated to high-load servlets
    in our application. Once in a while, we get into a condition in which none of
    these threads are available and then the threads never become available - we have
    to restart the server.
    I realize that this is a pretty generic description of the problem :-) but I wonder
    if anyone else has encountered this and has an idea what might be causing it ?
    Right now I am guessing that something in our code causes a resource contention
    that eventually deadlocks all the threads. But that is just a guess.

    Ethan,
    "Ethan Allen" <[email protected]> wrote in message
    news:3e0220a1$[email protected]..
    Thanks, Dimitri and Slava !
    I will do this and learn a little emore ...FYI, there is a web site dedicated to weblogic documentation -
    http://e-docs.bea.com/
    Pick your server version, go to "Search", type "thread dump".
    Regards,
    Slava Imeshev
    >
    ethan
    "Slava Imeshev" <[email protected]> wrote:
    Hi Ethan,
    For windows press <Ctrl>+<Break> in the server shell window,
    for *nix send kill -3 {server PID}.
    Regards,
    Slava Imeshev
    "Ethan Allen" <[email protected]> wrote in message
    news:3e020fb4$[email protected]..
    Thanks for your reply, Dimitri.
    We have not looked at thread dumps. How may we do this ?
    Ethan
    "Dimitri I. Rakitine" <[email protected]> wrote:
    Did you try looking at thread dumps when this happens ?
    Ethan Allen <[email protected]> wrote:
    We've set up thread pools for several execute queues dedicated to
    high-load
    servlets
    in our application. Once in a while, we get into a condition in
    which
    none of
    these threads are available and then the threads never become
    available
    - we have
    to restart the server.
    I realize that this is a pretty generic description of the problem:-) but I wonder
    if anyone else has encountered this and has an idea what might be
    causing
    it ?
    Right now I am guessing that something in our code causes a
    resource
    contention
    that eventually deadlocks all the threads. But that is just a
    guess.
    >>>>
    Dimitri

  • 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.

  • Thread pool throws reject exceptions even though the queue is not full

    Hi. I am using org.springframework.scheduling.concurrent.ThreadPo olTaskExecutor which is based on java
    java.util.concurrent.ThreadPoolExecutor
    with a enviornment under load.
    I see on some cases, that this thread pool throws tasks with reject exception
    even though the queue size is 0.
    According to the documentation, this thread pool should increase the pool size to core size and then wait untill all queue is full to create new threads.
    this is not what happends. for some reason the queue is not filled but exceptions are thrown.
    Any ideas why this can happen?

    This is the stack trace:
    taskExecutorStats-1 2010-04-27 11:01:43,324 ERROR [com.expand.expandview.infrastructure.task_executor] TaskExecutorController: RejectedExecutionException exception in thread: 18790970, failed on thread pool: [email protected]544f07, to run logic: com.expand.expandview.infrastructure.logics.DispatchLogicsProviderLogic
    org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@dd9007] did not accept task: com.expand.expandview.infrastructure.task_executor.TaskExecuterController$1@141f728; nested exception is java.util.concurrent.RejectedExecutionException
         at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:305)
         at com.expand.expandview.infrastructure.task_executor.TaskExecuterController.operate(TaskExecuterController.java:68)
         at com.expand.expandview.infrastructure.proxies.DataProxy.callLogic(DataProxy.java:131)
         at com.expand.expandview.infrastructure.proxies.DataProxy.operate(DataProxy.java:109)
         at com.expand.expandview.infrastructure.logics.AbstractLogic.operate(AbstractLogic.java:455)
         at com.expand.expandview.server.app.logics.stats.StatsPersisterSingleChunkLogic.persistSlots(StatsPersisterSingleChunkLogic.java:362)
         at com.expand.expandview.server.app.logics.stats.StatsPersisterSingleChunkLogic.doLogic(StatsPersisterSingleChunkLogic.java:132)
         at com.expand.expandview.infrastructure.logics.AbstractLogic.execute(AbstractLogic.java:98)
         at com.expand.expandview.server.app.logics.ApplicationLogic.execute(ApplicationLogic.java:79)
         at com.expand.expandview.infrastructure.task_executor.TaskExecuterControllerDirect.operate(TaskExecuterControllerDirect.java:33)
         at com.expand.expandview.infrastructure.proxies.LogicProxy.service(LogicProxy.java:62)
         at com.expand.expandview.infrastructure.logics.AbstractLogic.service(AbstractLogic.java:477)
         at com.expand.expandview.server.app.logics.stats.StatsPersisterLogic.persist(StatsPersisterLogic.java:48)
         at com.expand.expandview.server.app.logics.stats.StatsPersisterLogic.doLogic(StatsPersisterLogic.java:19)
         at com.expand.expandview.infrastructure.logics.AbstractLogic.execute(AbstractLogic.java:98)
         at com.expand.expandview.server.app.logics.ApplicationLogic.execute(ApplicationLogic.java:79)
         at com.expand.expandview.infrastructure.task_executor.TaskExecuterController$1.run(TaskExecuterController.java:80)
         at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
         at java.lang.Thread.run(Thread.java:619)
    Caused by: java.util.concurrent.RejectedExecutionException
         at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1760)
         at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:767)
         at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:658)
         at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:302)
         ... 19 more

  • 100% thread pool/CPU usage prevent MII Applications from working

    Hello to all,
    our MII Applications were running on MII 11.5 (windowsserver 2003, with IIS) without any problems.
    We migrated these MII Applications  to SAP xMII Version 12.0 SP8, SAP NW 7.00 SP20, Java HotSpot(TM) 64-Bit Server VM (build 1.4.2_22-rev-b03, mixed mode).
    Since then we faced several problems that prevent the MII Applications from working, e.g.
    - 100% application thread pool usage rate
    - 100% CPU usage
    - awful lot of http connections
    Several sap support calls didn't find the root cause.
    Workaround: restart sap system weekly
    Does anyone has any idea how to go on?
    Thanks
    Simone

    Hi Mike,
    sap support told us to install this Java version.
    Furthermore SAP Note 716604 says "do not use J2SE 5.0" and "Currently, we recommend 1.4.2_24 b06".
    Any more ideas?
    Regards,
    Simone

  • JRun Thread Pool Issue

    I'm running CF 9.0.1 on Ubuntu on an "Medium" Amazon EC2 instance. CF has been crashing intermittently (several times per day). At such times, running top gets me this (or something similar):
    PID
    USER
    PR
    NI
    VIRT
    RES
    SHR
    S
    %CPU
    %MEM
    TIME+COMMAND                                                                                                   
    15855
    wwwrun
    20
    0
    1762m
    730m
    20m
    S
    99.3
    19.4
    13:22.96 coldfusion9
    So, it's obviously consuming most of the server resources. The following error has been showing up in my cfserver.log in the leadup to each crash:
    java.lang.RuntimeException: Request timed out waiting for an available thread to run. You may want to consider increasing the number of active threads in the thread pool.
    If I run /opt/coldfusion9/bin/coldfusion status, I get:
    Pg/Sec  DB/Sec  CP/Sec  Reqs  Reqs  Reqs  AvgQ   AvgReq AvgDB  Bytes  Bytes
    Now Hi  Now Hi  Now Hi  Q'ed  Run'g TO'ed Time   Time   Time   In/Sec Out/Sec
    0   0   0   0   -1  -1  150   25    0     0      -1352560      0      0
    In the administrator, under Server Settings > Request Tuning, the setting for Maximum number of simultaneous Template requests is 25. So this makes sense so far. I could just increase the thread pool to cover these sort of load spikes. I could make it 200. (Which I did just now as a test.)
    However, there's also this file /opt/coldfusion9/runtime/servers/coldfusion/SERVER-INF/jrun.xml. And some of the settings in there appear to conflict. For example, it reads:
    <service class="jrunx.scheduler.SchedulerService" name="SchedulerService">
      <attribute name="bindToJNDI">true</attribute>
      <attribute name="activeHandlerThreads">25</attribute>
      <attribute name="maxHandlerThreads">1000</attribute>
      <attribute name="minHandlerThreads">20</attribute>
      <attribute name="threadWaitTimeout">180</attribute>
      <attribute name="timeout">600</attribute>
    </service>
    Which a) has fewer active threads (what does this mean?), and b) has a max threads that exceed the simultaneous request limit set in the admin. So, I'm not sure. Are these independent configs that need to be made to match manually? Or is the jrun.xml file supposed to be written by the CF Admin when changes are made there? Hmm. But maybe this is different because presumably the CF Scheduler should only use a subset of all available threads, right...so we'd always have some threads for real live users. We also have this in there:
    <service class="jrun.servlet.http.WebService" name="WebService">
      <attribute name="port">8500</attribute>
      <attribute name="interface">*</attribute>
      <attribute name="deactivated">true</attribute>
      <attribute name="activeHandlerThreads">200</attribute>
      <attribute name="minHandlerThreads">1</attribute>
      <attribute name="maxHandlerThreads">1000</attribute>
      <attribute name="mapCheck">0</attribute>
      <attribute name="threadWaitTimeout">300</attribute>
      <attribute name="backlog">500</attribute>
      <attribute name="timeout">300</attribute>
    </service>
    This appears to have changed when I changed the CF Admin setting...maybe...but it's the activeHandlerThreads that matches my new maximum simulataneous requests setting...rather than the maxHandlerThreads, which again exceeds it. Finally, we have this:
    <service class="jrun.servlet.jrpp.JRunProxyService" name="ProxyService">
      <attribute name="activeHandlerThreads">200</attribute>
      <attribute name="minHandlerThreads">1</attribute>
      <attribute name="maxHandlerThreads">1000</attribute>
      <attribute name="mapCheck">0</attribute>
      <attribute name="threadWaitTimeout">300</attribute>
      <attribute name="backlog">500</attribute>
      <attribute name="deactivated">false</attribute>
      <attribute name="interface">*</attribute>
      <attribute name="port">51800</attribute>
      <attribute name="timeout">300</attribute>
      <attribute name="cacheRealPath">true</attribute>
    </service>
    So, I'm not certain which (if any) of these I should change and what exactly the relationship is between maximum requests and maximum threads. Also, since several of these list the maxHandlerThreads as 1000, I'm wondering if I should just set the maximum simultaneous requests to 1000. There must be some upper limit that depends on available server resources...but I'm not sure what it is and I don't really want to play around with it since it's a production environment.
    I'm not sure if it pertains to this issue at all, but when I run a ps aux | grep coldfusion I get the following:
    wwwrun   15853  0.0  0.0   8704   760 pts/1
    S
    20:22   0:00 /opt/coldfusion9/runtime/bin/coldfusion9 -jar jrun.jar -autorestart -start coldfusion
    wwwrun   15855  5.4 18.2 1678552 701932 pts/1  
    Sl
    20:22   1:38 /opt/coldfusion9/runtime/bin/coldfusion9 -jar jrun.jar -start coldfusion
    There are always these two and never more than these two processes. So there does not appear to be a one-to-one relationship between processes and threads. I recall from an MX 6.1 install I maintained for many years that additional CF processes were visible in the process list. It seemed to me at the time like I had a process for each thread...so either I was wrong or something is quite different in version 9 since it's reporting 25 running requests and only showing these two processes. If a single process can have multiple threads in the background, then I'm given to wonder why I have two processes instead of one...just curious.
    So, anyway, I've been experimenting while composing this post. As noted above I adjusted the maximum simulataneous requests up to 200. I was hoping this would solve my problem, but CF just crashed again (rather it slogged down and requests started timing out...so effectively "crashed"). This time, top looked similar (still consuming more than 99% of the CPU), but CF status looked different:
    Pg/Sec  DB/Sec  CP/Sec  Reqs  Reqs  Reqs  AvgQ   AvgReq AvgDB  Bytes  Bytes
    Now Hi  Now Hi  Now Hi  Q'ed  Run'g TO'ed Time   Time   Time   In/Sec Out/Sec
    0   0   0   0   -1  -1  0     150   0     0      0      0      0      0
    Obviously, since I'd increased the maximum simultaneous requests, it was allowing more requests to run simultaneously...but it was still maxing out the server resources.
    Further experiments (after restarting CF) showed me that the server became unusably slogged after about 30-35 "Reqs Run'g", with all additional requests headed for an inevitible timeout:
    Pg/Sec  DB/Sec  CP/Sec  Reqs  Reqs  Reqs  AvgQ   AvgReq AvgDB  Bytes  Bytes
    Now Hi  Now Hi  Now Hi  Q'ed  Run'g TO'ed Time   Time   Time   In/Sec Out/Sec
    0   0   0   0   -1  -1  0     33    0     0      -492   0      0      0
    So, it's clear that increasing the maximum simultaneous requests has not helped. I guess what it comes down to is this: What is it having such a hard time with? Where are these spikes coming from? Bursts of traffic? On what pages? What requests are running at any given time? I guess I simply need more information to continue troubleshooting. If there are long-running requests, or other issues, I'm not seeing it in the logs (although I do have that option checked in the admin). I need to know which requests exactly are those responsible for these spikes. Any help would be much appreciated. Thanks.
    ~Day

    I really appreciate your help. However, I haven't been able to find the JRun Thread settings you describe above.
    Under Request Tuning, I see:
    Server Settings > Request Tuning
    Request Limits
    Maximum number of simultaneous Template requests
      Restricts the number of simultaneously processed requests. Use this setting to increase overall system performance for heavy load applications. Requests beyond the specified limit are queued. On Standard Edition, you must restart ColdFusion to enable this setting. 
    Maximum number of simultaneous Flash Remoting requests
      The number of Flash Remoting requests that can be processed concurrently.
    Maximum number of simultaneous Web Service requests
      The number of Web Service requests that can be processed concurrently.
    Maximum number of simultaneous CFC function requests
      The number of ColdFusion Component methods that can be processed concurrently via HTTP. This does not affect invocation of CFC methods from within CFML, only methods requested via an HTTP request.
    Tag Limit Settings
    Maximum number of simultaneous Report threads
      The maximum number of ColdFusion reports that can be processed concurrently.
    Maximum number of threads available for CFTHREAD
      The maximum number of threads created by CFTHREAD that will be run concurrently. Threads created by CFTHREAD in excess of this are queued.  On Standard Edition, the maximum limit is 10. 
    And under Java and JVM, I see:
    Server Settings > Java and JVM
        Java and JVM settings control the way ColdFusion starts the Java Virtual Machine when it starts.  You can control settings like what classpaths are used and how memory is allocated as well as add custom command line arguments.  Changing these settings requires restarting ColdFusion.  If you enter an incorrect setting, ColdFusion may not restart properly. 
       Backups of the jvm.config file are created when you hit the submit button. You can use this backup to restore from a critical change. 
       Java Virtual Machine Path
      Specifies the location of the Java Virtual Machine.
       Minimum JVM Heap Size (MB)         Maximum JVM Heap Size  (MB)       
       The Memory Size settings determine the amount of memory that the JVM can use for programs and data. 
       ColdFusion Class Path
      Specifies any additional class paths for the JVM, with multiple directories separated by  commas.
       JVM Arguments
      -server -Dsun.io.useCanonCaches=false -XX:MaxPermSize=192m -XX:+UseParallelGC -Xbatch -Dcoldfusion.rootDir={application.home}/../ -Dcoldfusion.libPath={application.home}/../lib
      Specifies any specific JVM initialization options, separated by spaces.
    I did go take a look at FusionReactor and found it's not free (which would be fine, of course, if it would actually help). It looks like there's a fully functional demo, which is cool...but I've haven't been able to get it to install yet, so we'll see.
    Thanks again!
    ~Day
    (By the way, I've cross-posted this inquiry on StackOverflow. So if you're able to help me arrive at a solution you might want to answer there as well.)

  • Windows 2008 r2 Cluster not starting - "unable to create security manager worker queues"

    Hello, following a power outage, we got a serious cluster error preventing the start of the cluster.
    We are trying to interpret the only four lines the cluster.log generates :
    00000330.000016cc::2014/09/26-10:44:06.348 ERR   [WTQ] bogus file creation failed, 2
    00000330.000016cc::2014/09/26-10:44:06.348 ERR   [WTQ] bogus file creation failed, 2
    00000330.000016cc::2014/09/26-10:44:06.348 ERR   [CS] Unable to create SecurityManager worker queues, 2
    00000330.000016cc::2014/09/26-10:44:06.363 ERR   Error 6
    AND if starting clussvc manually :
    Got ERROR_FILE_NOT_FOUND(2)' because of 'Error while creating the Security Manag
    er's Thread Pool' in
        000007fe:fd69940d( ERROR_MOD_NOT_FOUND(126) )
        00000000:001ff190( ERROR_MOD_NOT_FOUND(126) )
    We suspect a DLL problem (because of mod not found), but we are unable to find the ones involved even with process monitor.
    clusdb hive seems ok.
    The situation is serious, can anybody help, please ?

    Hi RodV,
    This error usually caused by cluster service fails to open a 
    handle to the \NUL device, Device manager shows the device instance in error state.
    Please check whether the following register value still exist, if not please backup your current registry then add the it.
    HKEY_LOCAL_MACHINE\SYSTEM\CURRENTCONTROLSET\ENUM\ROOT\LEGACY_NULL\0000\CONTROL
    ActiveService REG_SZ Null
    I am glad to be of help to you!
    We
    are trying to better understand customer views on social support experience, so your participation in this
    interview project would be greatly appreciated if you have time.
    Thanks for helping make community forums a great place.

  • Thread pool rejecting threads when I don't think it should, ideas?

    Hi,
    I have a server application in which I only want a specific number of simultaneous requests. If the server gets more then this number it is suppose to close the connection (sends an HTTP 503 error to the client). To do this I used a fix thread pool. When I start the server and submit the max number of requests I get the expected behavior. However if I resubmit the request (within a small period of time, e.g. 1-15 seconds after the first one) I get very odd behavior in that some of the requests are rejected. For example if I set the max to 100 the first set of requests will work fine (100 requests, 100 responses). I then submit again and a small number will be rejected (I've seen it range from 1 to 15 rejected)....
    I made a small app which kind of duplicates this behavior (see below). Basically when I see is that the first time submitting requests works fine but the second time I get a rejected one. As best as I can tell none should be rejected....
    Here is the code, I welcome your thoughts or if you see something I am doing wrong here...
    <pre>
    import java.util.concurrent.*;
    import java.util.concurrent.atomic.AtomicInteger;
    public class ThreadPoolTest {
         static AtomicInteger count = new AtomicInteger();
         public static class threaded implements Runnable {
              @Override
              public void run() {
                   System.out.println("In thread: " + Thread.currentThread().getId());
                   try {
                        Thread.sleep(500);
                   } catch (InterruptedException e) {
                        System.out.println("Thread: " + Thread.currentThread().getId()
                                  + " interuptted");
                   System.out.println("Exiting run: " + Thread.currentThread().getId());
         private static int maxThreads = 3;
         private ThreadPoolExecutor pool;
         public ThreadPoolTest() {
              super();
              pool = new java.util.concurrent.ThreadPoolExecutor(
                        1, maxThreads - 1, 60L, TimeUnit.SECONDS,
                        new ArrayBlockingQueue<Runnable>(1));
         public static void main(String[] args) throws InterruptedException {
              ThreadPoolTest object = new ThreadPoolTest();
              object.doThreads();
              Thread.sleep(3000);
              object.doThreads();
              object.pool.shutdown();
              try {
                   object.pool.awaitTermination(60, TimeUnit.SECONDS);
              } catch (InterruptedException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
         private void doThreads() {
              int submitted = 0, rejected = 0;
              int counter = count.getAndIncrement();
              for (int x = 0; x < maxThreads ; x++) {
                   try {
                        System.out.println("Run #: " + counter + " submitting " + x);
                        pool.execute(new threaded());
                        submitted++;
                   catch (RejectedExecutionException re) {
                        System.err.println("\tRun #: " + counter + ", submission " + x
                                  + " was rejected");
                        System.err.println("\tQueue active: " + pool.getActiveCount());
                        System.err.println("\tQueue size: " + pool.getPoolSize());
                        rejected++;
              System.out.println("\n\n\tRun #: " + counter);
              System.out.println("\tSubmitted: " + (submitted + rejected));
              System.out.println("\tAccepted: " + submitted);
              System.out.println("\tRejected: " + rejected + "\n\n");
    </pre>

    First thank you for taking the time to reply, I do appreciate it.
    jtahlborn - The code provided here is a contrived example trying to emulate the bigger app as best as I could. The actual program doesn't have any sleeps, the sleep in the secondary thread is to simulate the program doing some work & replying to a request. The sleep in the primary thread is to simulate a small delay between 'requests' to the pool. I can make this 1 second and up to (at least) 5 seconds with the same results. Additionally I can take out the sleep in the secondary thread and still see the a rejection.
    EJP - Yes I am aware of the TCP/IP queue, however; I don't see that as relevant to my question. The idea is not to prevent the connection but to respond to the client saying we can't process the request (send an "HTTP 503" error). So basically if we have, say, 100 threads running then the 101st, connection will get a 503 error and the connection will be closed.
    Also my test platform - Windows 7 64bit running Java 1.6.0_24-b07 (32bit) on an Intel core i7.
    It occurred to me that I did not show the output of the test program. As the output shows below, the first set of requests are all processed properly. The second set of requests is not. The pool should have 2 threads and 1 slot in the queue, so by the time the second "request" is made at least 2 of the requests from the first call should be done processing, so I could possibly understand run 1, submit #2 failing but not submit 1.
    <pre>
    Run #: 0 submitting 0
    Run #: 0 submitting 1
    Run #: 0 submitting 2
    In thread: 8
    In thread: 9
    Exiting run: 8
    Exiting run: 9
         Run #: 0
         Submitted: 3
         Accepted: 3
         Rejected: 0
    In thread: 8
    Exiting run: 8
    Run #: 1 submitting 0
    In thread: 9
    Run #: 1 submitting 1
         Run #: 1, submission 1 was rejected
         Queue active: 1
         Queue size: 2
    Run #: 1 submitting 2
         Run #: 1
         Submitted: 3
         Accepted: 2
         Rejected: 1
    In thread: 8
    Exiting run: 9
    Exiting run: 8
    </pre>

  • Thread pool in servlet container

    Hello all,
    I'm working on this webapp that has some bad response times and I've identified an area were we could shave off a considerable amount of time. The app is invoking a component that causes data to be catched for subsequently targeted apps in the environemnt. Our app does not need to wait for a response so I'd like to make this an asyncronous call. So, how best to implement this?...I considered JMS, but started working on a solution using the Java 1.4 backport of JSR 166 (java.util.concurrent).
    I've been testing the use of a ThreadPoolExecutor, using an ArrayBlockingQueue. The work that each Runnable will perform involves a lot of waiting (the component we call invokes a web service, among a couple other distributed calls). So I figure the pool will be much larger than the queue. Our container has 35 execute threads, so I've been testing with a thread pool size of 25, and a queue of 10.
    Any thoughts on this approach? I understand that some of this work could be simplified by JMS, but if I don't need to be tied to the container, I'd prefer not to. The code if much easier to unit test, and plays nicely with our continious build integration (which runs our junit test for us and notifies on errors).
    Any thoughts are greatly appreciated...Thanks!!

    Well, if it works, that's by far the best way to go - but note that creating threads in a servlet container means those threads are outside of the container's control. Many containers will refuse to give the new threads access to the JNDI context, even, and some may prevent you from creating threads at all.

  • BEA-002900 Initializing self-tuning thread pool   HANGS

    Hi All,
    Trying to start a WLS 10.3.5 instance using jrockit and it is hanging on:
    Apr 6, 2013 2:08:39 AM CDT> <Info> <WorkManager> <BEA-002900> <Initializing self-tuning thread pool>
    end of trace
    I took a thread dump and see this:
    "[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'" id=15 idx=0x3c tid=3810 prio=5 alive, waiting, native_blocked, daemon
    -- Waiting for notification on: weblogic/work/ExecuteThread@0xa0c21480[fat lock]
    at jrockit/vm/Threads.waitForNotifySignal(JLjava/lang/Object;)Z(Native Method)
    at java/lang/Object.wait(J)V(Native Method)
    at java/lang/Object.wait(Object.java:485)
    at weblogic/work/ExecuteThread.waitForRequest(ExecuteThread.java:162)
    ^-- Lock released while waiting: weblogic/work/ExecuteThread@0xa0c21480[fat lock]
    at weblogic/work/ExecuteThread.run(ExecuteThread.java:183)
    at jrockit/vm/RNI.c2java(JJJJJ)V(Native Method)
    -- end of trace
    "JFR request timer" id=16 idx=0x40 tid=3811 prio=5 alive, waiting, native_blocked, daemon
    -- Waiting for notification on: java/util/TaskQueue@0xa0c20b28[fat lock]
    at jrockit/vm/Threads.waitForNotifySignal(JLjava/lang/Object;)Z(Native Method)
    at java/lang/Object.wait(J)V(Native Method)
    at java/lang/Object.wait(Object.java:485)
    at java/util/TimerThread.mainLoop(Timer.java:483)
    ^-- Lock released while waiting: java/util/TaskQueue@0xa0c20b28[fat lock]
    at java/util/TimerThread.run(Timer.java:462)
    at jrockit/vm/RNI.c2java(JJJJJ)V(Native Method)
    -- end of trace
    ===== END OF THREAD DUMP ===============
    Done:
    1). Cleared TMP and CACHE directories and still does not solve the issue.
    Any ideas are appreciated.
    thanks

    Hi,
    we had same hang issue. Unfortunately we run behind VM.
    It was an issue with securerandom generation.
    We solved the issue as described here http://stackoverflow.com/a/2564406/2098832
    Also deleted the <server>/tmp directory before.
    Hope this helps.
    Casey

  • 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;       
    }

  • Pattern for Thread Pool?

    Hi
    i want to build a kind of download manager. The application should be able to handle some concurrent threads, each representing a download in progress.
    I thought i might be more efficient to reuse a download thread after the download has ended as to create a new thread each time (like the connection object for db queries). Is this right? If yes, i thought to build a thread pool that serves a limited number of threaded download objects as requested (am I on the right way?).
    Now, I have to basic problems: (a) is it right, that, if the run() method of a thread has ended, the whole thread gets destroved? if yes, how should i prevent the thread from being destroyed, so i can reuse it later on? Second (b) how would that pool mechnism look like, means, there must be some kind of vector where i put in and take out the threads.
    As you see, these are basic "pool" technique questions. So, I thought, maybe there is a design pattern that would give me the basic mechanism, Does anyone know such a pattern?
    Thanks for your help
    josh

    I thought i might be more efficient to reuse a
    download thread after the download has ended as to
    create a new thread each time (like the connection
    object for db queries). Is this right? If yes, iIt may be right, if creating new threads is wasting enough CPU cycles to justify the complication of a thread pool. Maybe for a high-load server it would be more efficient. You'll have to figure that out for your own specific application.
    Another good use for thread pools is to avoid putting time-consuming operations in ActionListeners, etc. Instead you can have them pass the task off to a thread pool, keeping the GUI responsive.
    Now, I have to basic problems: (a) is it right, that,
    if the run() method of a thread has ended, the whole
    thread gets destroved? if yes, how should i prevent
    the thread from being destroyed, so i can reuse it
    later on? Second (b) how would that pool mechnism look
    like, means, there must be some kind of vector where i
    put in and take out the threads. (a) You are right. Therefore, the worker threads should not exit their run() methods until interrupted. (b) Worker threads could check a job queue (containing Runnables, perhaps) and if there are none, they should wait() on some object. When another thread adds a new job to the queue, it should call notify() on the same object, thus waking up one of the worker threads to perform the task.
    I wrote a thread pool once, just as an exercise. You will run into a number of problems and design issues (such as, what should the worker threads do when interrupted, exit immediately or clear the job queue and then exit?) If you have any more questions, ask in this thead.
    Krum

  • On Threads, Pools and other beasts

    I'm seeing some unexpected behaviours in our production system relating to
    (AFAICS) threads and connection pools, and hope you could bring some info
    about them.
    Our system is built over 4 PIII Xeon processors under Linux.
    We do currently have this configuration:
    15 threads for the "default" queue and 10 for an special servlet one, we
    decided to separate threads in two queues in order to assure our users get
    always a thread besides what the rest of the system is "doing".
    Even we do not have a lot of users (about 20 or so) they do generate a lot
    of load as for the bussiness logic inherent to the application.
    For sample, so you can understand what goes with practically any user
    "action", think on this.
    After a user "confirms" some data via a servlet and after executing the data
    validation and bussines rules some messages are sent via JMS to the
    "asynchronous" part of the system (that is running in the same weblogic
    instance). After commiting the user transaction an thus releasing the
    servlet thread, so it can be used by the same or other user, JMS messages
    are delivered to MDBs that must transform information from on-line (servlet)
    processes in different ways so they can be stored onto other systems, i.e.
    into a mainframe, into an XML DB and possibly into another RDBMS. Our
    configuration is that there can be as much as 10 MDB of each type (I mean
    for each kind of "action" of a servlet) running concurrently and as you can
    suppose those processes do take some time to communicate with destination
    systems and perform their work.
    We end at last with a lot of concurrent processes in our system that ends
    some time with the users complaining about system responsiveness.
    After all this explanation I would like to know if 25 threads for
    "background" and on-line processes is too low (as I'm afraid they are). The
    problem is we can't seem to increase the number of threads without being
    very careful with JDBC connection pools.
    Currently we have two connection pools. We do demarcate transactions in the
    clients (servlets, batch processes) we have a "transacted" pool and a "non
    transacted" one.
    We are delegating persistence to the contanier (formally in our case we are
    using TopLink persistence and it uses in it's deployment descriptor both
    types of pools)
    Our configuration is as follows:
    Oracle pool NON Tx 60 connections
    Oracle pool Tx 30 connections
    initially we create 5 connections for each pool with an increment of 5 for
    each one too.
    From the tests I have made I have discovered that setting more threads than
    the minimum amount of pools yields to this exception:
    weblogic.common.ResourceException: No available connections in pool
    myNonTxPool
    at
    weblogic.common.internal.ResourceAllocator.reserve(ResourceAllocator.java:57
    8)
    at
    weblogic.common.internal.ResourceAllocator.reserve(ResourceAllocator.java:40
    5)
    at
    weblogic.common.internal.ResourceAllocator.reserveNoWait(ResourceAllocator.j
    ava:373)
    at
    weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:165
    at
    weblogic.jdbc.common.internal.ConnectionPool.reserveNoWait(ConnectionPool.ja
    va:126)
    at
    weblogic.jdbc.common.internal.RmiDataSource.getPoolConnection(RmiDataSource.
    java:194)
    at
    weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java
    :219)
    the behaviour I would expect is that in case a thread needs a connection and
    there isn't any one available the thread may be blocked and it would receive
    the connecion once one is released, of course as I can see in the stack the
    ConnectionPool.reserveNoWait() method is behaving just the other way.
    The main problem with this is that as you can see we are "forced" to spend
    90 (60+30) connections to the DB (even we will never use more than 25
    (15+10) simultaneously just because we must assure that at least there is
    one "reserved" connection to each thread.
    Our DBA thinks that it can't be possible that we spend such number of
    connections that could be taken by another application(s) (as the DB is
    shared with other apps)
    Currently our DB system is not set as "multithreaded" so each connection
    created against the DB is a process on the system and of course they are a
    really scarce resource.
    My question is. What would be a "fine" number of threads for an application
    like this that is mainly "background-batch processing" but assuring on-line
    users have their threads always available?
    I have just another doubt (maybe this is not the right thread to ask for it
    but...) how does the UserTransaction actually works? I mean, is the
    connection given to the thread (and thus extracted from pool) as soon as the
    thread begin it's work? or is it given in the instant of "commiting" to the
    DB. I know maybe using TopLink changes default Weblogic CMP behaviour but I
    would like to know what the "default" Weblogic behaviour is; and, what
    happens when you don't start a transaction in the client and total execution
    time exceeds 30 seconds? I have seen a rollback due to "exceeding" those 30
    seconds althought I'm sure we do not open any transaction, what kind of
    "transaction" is that? Is just a way of Weblogic to assure a thread is not
    "locked" more than a certain period of time so the system never "stalls"?
    Thanks in advance.
    Regards.
    Ignacio.

    Hi Ignacio,
    See my answer inline.
    "Ignacio G. Dupont" <[email protected]> wrote in message
    news:[email protected]...
    I'm seeing some unexpected behaviors in our production system relating to
    (AFAICS) threads and connection pools, and hope you could bring some info
    about them.
    Our system is built over 4 PIII Xeon processors under Linux.
    We do currently have this configuration:
    15 threads for the "default" queue and 10 for an special servlet one, weThat numbers defines number of concurrent requests services
    associated with that queues can handle. If monitoring CPU utilization
    shows that CPU load is not high, let say less than 90%, - you can increase
    that numbers.
    decided to separate threads in two queues in order to assure our users get
    always a thread besides what the rest of the system is "doing".
    Even we do not have a lot of users (about 20 or so) they do generate a lot
    of load as for the bussiness logic inherent to the application.>
    For sample, so you can understand what goes with practically any user
    "action", think on this.[eaten]
    We end at last with a lot of concurrent processes in our system that ends
    some time with the users complaining about system responsiveness.You will have to run a load test in your QA environment and play with queue
    sizes. In addition, you may want to run a profiler (like JProbe or
    OptimizeIt)
    for maximum load to find if there are bottlenecks in the application.
    After all this explanation I would like to know if 25 threads for
    "background" and on-line processes is too low (as I'm afraid they are).The
    It all depends of the usage pattern. I'd say that for a production
    environment
    with any noticeable load, it's low.
    problem is we can't seem to increase the number of threads without being
    very careful with JDBC connection pools.Yes, you will have to increase size of the pools to match maximum
    number of ongoing transactions. Minimum would be a number of execution
    threads. Actual number should be determined either by load testing or
    by setting it to a guaranteed high level.
    Currently we have two connection pools. We do demarcate transactions inthe
    clients (servlets, batch processes) we have a "transacted" pool and a "non
    transacted" one.
    We are delegating persistence to the contanier (formally in our case weare
    using TopLink persistence and it uses in it's deployment descriptor both
    types of pools)
    Our configuration is as follows:
    Oracle pool NON Tx 60 connections
    Oracle pool Tx 30 connections
    initially we create 5 connections for each pool with an increment of 5 for
    each one too.
    From the tests I have made I have discovered that setting more threadsthan
    the minimum amount of pools yields to this exception:That's quite natural.
    weblogic.common.ResourceException: No available connections in pool
    myNonTxPool[eaten]
    the behaviour I would expect is that in case a thread needs a connectionand
    there isn't any one available the thread may be blocked and it wouldreceive
    That would lock exec threads very quickly. A connection pool is a vital
    resource that is to be available constantly. So weblogic uses fail-fast
    approach so that you can adjust setting to match highest load.
    the connecion once one is released, of course as I can see in the stackthe
    ConnectionPool.reserveNoWait() method is behaving just the other way.>
    The main problem with this is that as you can see we are "forced" to spend
    90 (60+30) connections to the DB (even we will never use more than 25
    (15+10) simultaneously just because we must assure that at least there is
    one "reserved" connection to each thread.That's right.
    Our DBA thinks that it can't be possible that we spend such number of
    connections that could be taken by another application(s) (as the DB is
    shared with other apps)I don't think it's a correct observation. Oracle can be configured to handle
    more connections. I saw weblogic pools configured to handle 200
    connections.
    Currently our DB system is not set as "multithreaded" so each connection
    created against the DB is a process on the system and of course they are a
    scarce resource.Application demand for resources should be satisfied.
    My question is. What would be a "fine" number of threads for anapplication
    like this that is mainly "background-batch processing" but assuringon-line
    users have their threads always available?It should be high enough to satisfy requirement to handle given number
    of concurrent requests processed on given hardware. Normally this
    is determined by load testing and gradual increase of this number
    to the point where you see that hardware (seen as CPU load)
    cannot handle it. Buy the way this point sometimes is unreachable
    as application becomes DB-bound, i.e. bottleneck is shifted to
    the database.
    I have just another doubt (maybe this is not the right thread to ask forit
    but...) how does the UserTransaction actually works? I mean, is the
    connection given to the thread (and thus extracted from pool) as soon asthe
    thread begin it's work? or is it given in the instant of "committing" tothe
    It's given when a connection, assuming it's obtained from TxDatasource,
    is requested.
    DB. I know maybe using TopLink changes default Weblogic CMP behavior but I
    would like to know what the "default" Weblogic behavior is; and, what
    happens when you don't start a transaction in the client and totalexecution
    time exceeds 30 seconds? I have seen a rollback due to "exceeding" those30
    seconds although I'm sure we do not open any transaction, what kind of
    "transaction" is that? Is just a way of Weblogic to assure a thread is notFor instance, stateful session beans are transactional.
    "locked" more than a certain period of time so the system never "stalls"?Basically, no, it's not. There is no way to "unlock" thread after certain
    period.
    So when a queue has finished processing, TX monitor checks the timeout,
    and if there is one, issues a corresponding rollback. So, it's possible
    for thread to run for 10 hours if the timeout is 30 seconds.
    Since 7.0 weblogic is capable of detecting such situations so that
    administrator can be informed about it and required actions can be
    taken [on application side].
    Hope this helps.
    Regards,
    Slava Imeshev

  • Thread pool configuration for write-behind cache store operation?

    Hi,
    Does Coherence have a thread pool configuration for the Coherence CacheStore operation?
    Or the CacheStore implementation needs to do that?
    We're using write-behind and want to use multiple threads to speed up the store operation (storeAll()...)
    Thanks in advance for your help.

    user621063 wrote:
    Hi,
    Does Coherence have a thread pool configuration for the Coherence CacheStore operation?
    Or the CacheStore implementation needs to do that?
    We're using write-behind and want to use multiple threads to speed up the store operation (storeAll()...)
    Thanks in advance for your help.Hi,
    read/write-through operations are carried out on the worker thread (so if you configured a thread-pool for the service the same thread-pool will be used for the cache-store operation).
    for write-behind/read-ahead operations, there is a single dedicated thread per cache above whatever thread-pool is configured, except for remove operations which are synchronous and still carried out on the worker thread (see above).
    All above is of course per storage node.
    Best regards,
    Robert

  • Thread Pool in JRT

    Hi All,
    1) the first question is what is the recommended way to allocate thread poll in rt system??
    2) what is the best way to do:
    -if my mission take a long time can i just suspend the thread and than resume it again (is it possible at all??)
    or the other option is to return it to the pool and get it again and again.
    im asking the second question from the view of performance and low latency .
    i assumed that each time i return the thread to the pool and than active it again it will "cost" cpu time.
    hope my question is clear enough.
    TIA
    Gabi

    Hi Gabi,
    I'm not completely clear on your questions. You can create a thread pool in a RT system the same basic way as in non-RT but you need to deal with priorities correctly. One way to do this would be to have the pool threads run at maximum priority normally and then drop to the actual priority of a submitted work task. Another model is to split the pool into groups based on priority, so effectively there is a work queue per priority-level and a group of threads to service each pool. This second model is used in the Real-time CORBA "lanes" approach. Designing an effective thread pool is a non-trivial task (take a look at the java.util.concurrent Thread PoolExecutor) and dealing with RT priorities and RT latency issues makes it even more complex.
    Alternatively rather than use an explicit thread pool consider whether you can convert the design to one using AsyncEvents and AsyncEventHandlers.
    Question 2 I don't really understand. Pool threads typically block on a queue waiting for work tasks to appear. But you can also have a non-terminating task that effectively picks up its own work from elsewhere - eg a task that reads from a socket to get the next "job" to process. Any interaction between threads is going to cost CPU time, whether a pool is involved or not, but yes there will be overhead associated with a pool, just as there is starting and terminating a thread, or performing any kind of thread coordination/synchronization.
    Regards,
    David Holmes

Maybe you are looking for