协调两个同步适配器的信号量会导致InterruptedException



我有两个同步适配器,我希望每一个都等到另一个完成后再开始执行。我在信号灯上使用了以下技术:

public class SyncerOne extends AbstractThreadedSyncAdapter {
    protected static Semaphore semaphore = new Semaphore(1);
...
    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)
    {
            semaphore.acquire();
            doMyStuff();
            semaphore.release();
    }
    public static Semaphore getSemaphore() {
        return semaphore;
    }

public class SyncerTwo extends AbstractThreadedSyncAdapter {
...
    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)
    {
            Semaphore semaphore = SyncerOne.getSemaphore();
            semaphore.acquire();
            doMyStuff();
            semaphore.release();
    }

当没有人需要等待时,一切都很好,但一旦其中一个同步适配器需要在Acquired()中等待,就会引发InterruptedException:

10-29 19:08:38.777: W/System.err(419): java.lang.InterruptedException
10-29 19:08:38.787: W/System.err(419):  at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1280)

有什么想法吗?

尝试使用Semaphore.acquireUnterruptibly()而不是Semaphore.acquire().

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html#acquireUninterruptibly()

相关内容

  • 没有找到相关文章