信号量不调用 release()

Semaphore does not invoke release()

我在每分钟调用的 Android 应用程序中实现了一个线程,该进程的调用通过警报管理器进行。

@Override
public void onReceive(Context context, Intent intent) {
    try {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wakeLock;
        if (powerManager != null) {
            wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Send-data");
            wakeLock.acquire(10 * 60 * 1000L);
            new Thread(new SendPolicyData(context)).start();
            wakeLock.release();
        }
    } catch (Exception e) {
        wil.WriteFile("1)AlarmSendData - Exception: " + e.toString());
    }
}

线程中包含的代码从数据库中提取一组数据,这些数据必须通过帖子发送到服务器,对资源的访问是通过信号量控制的。

@SuppressWarnings("ResultOfMethodCallIgnored")
public class SendPolicyData implements Runnable {
    private static final WriteInLogFile wil = new WriteInLogFile();
    private final Context ctx;

public SendPolicyData(Context ctx) {
    this.ctx = ctx;
}

public void run() {
    try {
        SemaphoreDTS.getInstance().goIn();

        Authentication singleCall = new Authentication();
        Utilities utilities = new Utilities(ctx);
        DbGest dbGest = DbGest.getInstance(ctx);

        if (utilities.checkConnection()) {
            int lastProcessedID = -1;
            int attempts = 0;

            Cursor cursor = dbGest.freeQuery("SELECT id, label, value, priority FROM " + TABLE_DATATOSEND + " ORDER BY priority,dateIns");
            if (cursor != null) {
                cursor.moveToFirst();
                if (cursor.getCount() > 0) {
                    do {
                        int toProcessID = cursor.getInt(cursor.getColumnIndex("id"));
                        String value = cursor.getString(cursor.getColumnIndex("value"));
                        String labelString = cursor.getString(cursor.getColumnIndex("label"));

                        if (lastProcessedID == toProcessID) {
                            if (attempts <= 5) {
                                attempts++;
                                Thread.sleep(3000);
                            } else {
                                attempts = 0;
                                dbGest.changeDTSRecordPriority(toProcessID);
                            }
                        }

                        switch (labelString) {
                            case "photo":
                               //DO STUFF
                             break;
                        lastProcessedID = toProcessID;
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        }
    } catch (Exception e) {
        SemaphoreDTS.getInstance().goOut();
        wil.WriteFile("7)SendPolicyData - Exception: " + e.toString());
    } finally {
        SemaphoreDTS.getInstance().goOut();
    }
    SemaphoreDTS.getInstance().goOut();
}

}

这是我用来管理资源访问的信号量的源代码。

public class SemaphoreDTS {
    private static SemaphoreDTS instance;
    private final Semaphore semaphore;
    private final WriteInLogFile wil = new WriteInLogFile();

private SemaphoreDTS() {
    this.semaphore = new Semaphore(1, true);
}

public static SemaphoreDTS getInstance() {
    if (instance == null) {
        instance = new SemaphoreDTS();
    }
    return instance;
}

public synchronized void goIn() {
    try {
        semaphore.acquire();
    } catch (Exception e) {
        wil.WriteFile("1)SemaphoreDTS - Exception: " + e.toString());
    }
}

public synchronized void goOut() {
    try {
        semaphore.release();
    } catch (Exception e) {
        wil.WriteFile("2)SemaphoreDTS - Exception: " + e.toString());
    }
}
}

在我做的测试中,信号量经常被阻塞,出于某种原因,必要的释放没有被调用以便能够执行新的获取。

我觉得我写的代码是正确的,我不明白我哪里错了。

在此代码块中:

catch (Exception e) {
        SemaphoreDTS.getInstance().goOut();
        wil.WriteFile("7)SendPolicyData - Exception: " + e.toString());
    } finally {
        SemaphoreDTS.getInstance().goOut();
    }
    SemaphoreDTS.getInstance().goOut();

你总是有两个调用 .goOut(),因为 finally 块总是会调用。 当您调用 .release() (在您的方法 .goOut() 中)时,信号量会获得可用的许可,即 1 个许可您的信号量会获得 2 个许可。我认为问题从这里开始。尝试在所有地方删除调用 `.goOut() 方法但不在 finally()