Merk
Merk

Reputation: 1486

'Park'ing related deadlock in FutureTask submitted to ExecutorService (in Android)

I have code of the following form:

ExecutorService execSvc = Executors.newFixedThreadPool(10);
...
execSvc.execute(new Runnable() {
    @Override
    public void run() {
        Thread.currentThread().setName("Wonky");

        try {
            //here is reached

            doSomethingWith(mSomeFutureTask.get());

            //here is never reached
        } catch (InterruptedException e) {
        ...
        }
    });

I also reduced mSomeFutureTask for testing purposes to a mere stub somewhat like this:

mSomeFutureTask = new FutureTask<Object>(new Callable<Object> {
    public Object call() {
       return null;
    }
});

But the thread 'Wonky' never returns from get(). When I pause the JVM this is the stack dump for that thread:

Thread [<18> Wonky] (Suspended) 
    <VM does not provide monitor information>   
    Object.wait(long, int) line: not available [native method]  
    Thread.parkFor(long) line: 1231 
    Unsafe.park(boolean, long) line: 323    
    LockSupport.park(Object) line: 157  
    FutureTask$Sync(AbstractQueuedSynchronizer).parkAndCheckInterrupt() line: 813   
    FutureTask$Sync(AbstractQueuedSynchronizer).doAcquireSharedInterruptibly(int) line: 973 
    FutureTask$Sync(AbstractQueuedSynchronizer).acquireSharedInterruptibly(int) line: 1282  
    FutureTask$Sync.innerGet() line: 219    
    MyOwnClass$SomeFutureTaskClass(FutureTask).get() line: 82   
    MyOwnClass$Runnable.run() line: 152 
    Executors$RunnableAdapter.call() line: 442  
    FutureTask$Sync.innerRun() line: 305    
    FutureTask.run() line: 137  
    ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: 1076  
    ThreadPoolExecutor$Worker.run() line: 569   
    Thread.run() line: 856  

I have no idea what it could be waiting for. There is talk in another thread of a bad implementation of Park pre-JVM 6u21.

This brings up two other questions--what version of Java SDK does a given Android environment use (how to find out?) And what is the relationship between a "JVM 6u21" (is that a standard or an implementation?) and Dalvik 1.5.0, which is what my device is running?

Upvotes: 0

Views: 343

Answers (1)

ZhongYu
ZhongYu

Reputation: 19682

You probably never submitted mSomeFutureTask to any executor, so the task is never finished, and get() is awaiting forever.

Upvotes: 1

Related Questions