android source

BlockedQue Operation

리오파파 2025. 3. 18. 20:46

transaction data 를 que에 넣어 두었다가 특정시간 지나면 그 que 를 처리

 

 


private static BlockingQueue<NotifiedData> notificationQueue = null;
private Handler batchHandler;
private AtomicReference<NotifiedData> pendingNotification = new AtomicReference<>(null);

NotifiedData pending = null;

    notificationQueue = new LinkedBlockingQueue<>(MAX_QUEUE_SIZE);
    notificationProcessor = new NotificationProcessor(this);
    batchHandler = new Handler(Looper.getMainLooper());
        ...


        pending = pendingNotification.getAndSet(null);
        if (pending != null) {
            if (!notificationQueue.offer(pending)) {
                // If still full, store it again for the next notification
                pendingNotification.set(pending);
                batch_interval = 10;
                return;
            }
        }

    try {
        if (!notificationQueue.offer(nD)) {
            batchHandler.removeCallbacksAndMessages(null);
            batchHandler.postDelayed(this::processBatch, 0);
            batch_interval = 10;
            batchNotRunning = true;
            utils.logW(TAGE, "NOTI FULL " + nD.who +" " + nD.text);
            pendingNotification.set(nD);
        }
    } catch (RuntimeException e) {  // Handle unexpected system crashes
        utils.logW(TAGE, "RuntimeErr: NULL "+e);
        restartService();
        return;
    } catch (Exception e) {  // Catch all unexpected errors
        Log.e(TAGE, "Unexpected: X "+e);
        return;
    }
    if (batchNotRunning) {
        batchHandler.removeCallbacksAndMessages(null);
        batchHandler.postDelayed(this::processBatch, batch_interval);
    }
}

private void processBatch() {
        try {
            while (!notificationQueue.isEmpty()) {
                notificationProcessor.process(notificationQueue.poll());
            }
        } catch (Exception e) {
            //Handle exception
        } finally {
        }
}

 

'android source ' 카테고리의 다른 글

내 앱이 화면 전면에 있나 확인하는 법  (0) 2025.02.25
res > mipmap > filename 읽기  (0) 2024.12.10
TextView clicked animation  (0) 2024.12.03
textview touch color 변화  (0) 2024.10.24
Sound, media 대신 ring mode로 설정  (0) 2024.10.19