mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Preventing thread starvation in blocking operations #1863

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I've sub-classed ListenableFuture to wrap helper functions from the Futures 
class to make the futures slightly more composable, however, the issue I'm now 
facing is preventing potential thread starvation in a fixed worker thread pool 
(Executors.GLOBAL) in the example below due to someone accidentally blocking in 
a Function or AsyncFunction (thus potentially blocking all threads in this 
pool). 

Scala's Await.result uses "managed blocking". i.e., detect the blocking 
operation and temporarily spawn a new thread (see 
http://stackoverflow.com/a/13099594). Is there anything equivalent in 
Guava/Java? 

Is this the reason Futures.transform has the option to use the current thread? 

public <ReturnType> MFuture<ReturnType> flatMap(AsyncFunction<InputType, 
ReturnType> fn){
        return new MFuture<ReturnType>(Futures.transform(listenableFuture, fn, Executors.GLOBAL));
    }

    public <ReturnType> MFuture<ReturnType> map(Function<InputType, ReturnType> fn){
        return new MFuture<ReturnType>(Futures.transform(listenableFuture, fn, Executors.GLOBAL));
    }

Original issue reported on code.google.com by consiliu...@gmail.com on 10 Oct 2014 at 10:14

GoogleCodeExporter commented 9 years ago
I've solved this issue by changing from a FixedThreadPool to a 
CachedThreadPool, which is probably ok for my scenario, but looking for 
suggestions. 

Original comment by consiliu...@gmail.com on 10 Oct 2014 at 12:35

GoogleCodeExporter commented 9 years ago
The Java support for managed blocking that I'm aware of is in the JDK's 
ForkJoinPool. I don't believe we have anything related in Guava.

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinPool.Manag
edBlocker.html

As for applying that to your situation, I'm a bit over my head. You might try 
StackOverflow.

Original comment by cpov...@google.com on 10 Oct 2014 at 2:23

GoogleCodeExporter commented 9 years ago
Thanks, that's exactly what I was looking for. 

Original comment by consiliu...@gmail.com on 10 Oct 2014 at 3:17

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07