Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
BackgroundActionsTracker.java
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3package org.qtproject.qt.android;
4
5import java.util.concurrent.ConcurrentLinkedQueue;
6import java.util.concurrent.atomic.AtomicInteger;
7
8// Helper class to keep track of background actions and relay them when ready to be executed
9class BackgroundActionsTracker {
10 // For unlimited queue
11 private int m_maxActions = -1;
12 private final ConcurrentLinkedQueue<Runnable> m_backgroundActionsQueue = new ConcurrentLinkedQueue<>();
13 private final AtomicInteger m_actionsCount = new AtomicInteger(0);
14
15 public void setMaxAllowedActions(int maxActions) {
16 m_maxActions = maxActions;
17 }
18
19 public void enqueue(Runnable action) {
20 if (m_maxActions == 0 || action == null)
21 return;
22
23 if (m_maxActions > 0 && m_actionsCount.get() >= m_maxActions) {
24 // If the queue is full, remove the oldest action, then decrement the queue count.
25 m_backgroundActionsQueue.poll();
26 m_actionsCount.decrementAndGet();
27 }
28 m_backgroundActionsQueue.offer(action);
29 m_actionsCount.incrementAndGet();
30 }
31
32 public void processActions() {
33 Runnable action;
34 while ((action = m_backgroundActionsQueue.poll()) != null) {
35 m_actionsCount.decrementAndGet();
36 QtNative.runAction(action);
37 }
38 }
39
40 public int getActionsCount() {
41 return m_actionsCount.get();
42 }
43}
44
45