Threads issues

164 views
Skip to first unread message

Eugene Prokopiev

unread,
Jan 11, 2015, 1:46:54 PM1/11/15
to asyncht...@googlegroups.com
Hi,

I need to have some threads with AsyncHttpClient in each. My code looks like:

public class HttpClientApp {
   
    ExecutorService pool = Executors.newFixedThreadPool(25);
   
    public HttpClientApp() throws Exception {
        List<Future<String>> futures = new ArrayList<Future<String>>();
        for (int i=0;i<25;i++) {
            futures.add(pool.submit(new Callable<String>() {
                public String call() throws Exception {
                    try (AsyncHttpClient client = new AsyncHttpClient()) {
                        Future<Response> response = client.prepareGet("http://10.7.1.13:3000").execute();
                        return response.get().getStatusText();
                    }
                }
            }));
        }
        for (Future<String> future : futures)
            System.out.println(future.get());
        System.out.println("END");
    }
   
    public static void main(String[] args) throws Exception {
        new HttpClientApp();       
    }

}


On running it I see:

9:29:05 PM org.jboss.netty.channel.socket.nio.AbstractNioSelector
WARNING: Unexpected exception in the selector loop.
java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:714)
        at org.jboss.netty.util.HashedWheelTimer.start(HashedWheelTimer.java:273)
        at org.jboss.netty.util.HashedWheelTimer.newTimeout(HashedWheelTimer.java:337)
        at org.jboss.netty.channel.socket.nio.NioClientBoss$RegisterTask.run(NioClientBoss.java:185)
        at org.jboss.netty.channel.socket.nio.AbstractNioSelector.processTaskQueue(AbstractNioSelector.java:391)
        at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:315)
        at org.jboss.netty.channel.socket.nio.NioClientBoss.run(NioClientBoss.java:42)
        at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
        at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

^CJava HotSpot(TM) 64-Bit Server VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal SIGINT to handler- the VM may need to be forcibly terminated


or:

9:38:23 PM org.glassfish.grizzly.nio.SelectorRunner doSelect
SEVERE: doSelect exception
java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:714)
        at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:950)
        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1368)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.executeIoEvent(WorkerThreadIOStrategy.java:100)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.executeIoEvent(AbstractIOStrategy.java:89)
        at org.glassfish.grizzly.nio.SelectorRunner.iterateKeyEvents(SelectorRunner.java:414)
        at org.glassfish.grizzly.nio.SelectorRunner.iterateKeys(SelectorRunner.java:383)
        at org.glassfish.grizzly.nio.SelectorRunner.doSelect(SelectorRunner.java:347)
        at org.glassfish.grizzly.nio.SelectorRunner.run(SelectorRunner.java:278)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
        at java.lang.Thread.run(Thread.java:745)

^CJava HotSpot(TM) 64-Bit Server VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal SIGINT to handler- the VM may need to be forcibly terminated


So, I have such problems with NettyAsyncHttpProvider and GrizlyAsyncHttpProvider and no problems with JDKAsyncHttpProvider. Why can it be and how to use NIO instead of threads with NettyAsyncHttpProvider and GrizlyAsyncHttpProvider?

Excilys

unread,
Jan 11, 2015, 2:00:35 PM1/11/15
to asyncht...@googlegroups.com
This is very wrong. You should be creating one single client instance, not one per thread!


--
You received this message because you are subscribed to the Google Groups "asynchttpclient" group.
To unsubscribe from this group and stop receiving emails from it, send an email to asynchttpclie...@googlegroups.com.
To post to this group, send email to asyncht...@googlegroups.com.
Visit this group at http://groups.google.com/group/asynchttpclient.
For more options, visit https://groups.google.com/d/optout.

Eugene Prokopiev

unread,
Jan 12, 2015, 12:34:48 AM1/12/15
to asyncht...@googlegroups.com
This is very wrong. You should be creating one single client instance, not one per thread!

Thanks! What about selecting threads/nio model? Is it possible to configure?

Bongjae Chang

unread,
Jan 12, 2015, 1:11:40 AM1/12/15
to asyncht...@googlegroups.com
Hi Eugene,

I think you can use AsyncHttpClientConfig#Builder interface. Here is an example:
---
final AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
configBuilder.setExecutorService(yourThreadPoolExecutor);
final AsyncHttpClientConfig config = configBuilder.build();
final AsyncHttpClient asyncHttpClient = new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config);
---
Then, the AHC will use your fixed thread pool with workers.

And rarely, I think you can also choose a NIO model with customizing your provider configuration such as using TransportCustomizer interface of Grizzly provider. (https://grizzly.java.net/iostrategies.html)

Thanks.

Regards,
Bongjae Chang


Eugene Prokopiev

unread,
Jan 12, 2015, 1:29:58 AM1/12/15
to asyncht...@googlegroups.com
Thanks!


--
WBR,
Eugene Prokopiev
Reply all
Reply to author
Forward
0 new messages