0

Please help me resolve the problem.
I'm trying to send data from gui thread to another thread via a queue.
But I got a problem. While another thread is using queue, GUI thread add an object to the queue, the Gui thread will be blocked some minisecond. So GUI is not smooth.
My class is:

public enum AresManager {
MANAGER;
Queue<AresAction> actionsQueue = new LinkedList<AresAction>();

public synchronized void sendAction(Context context, AresAction action) {
    actionsQueue.add(action);
    Intent intent = new Intent(context, AresServiceSingleHandler.class);
    context.startService(intent);
}

public synchronized AresAction getActionFromQueue() {
    AresAction action = actionsQueue.poll();
    AresLog.v("[Actions Queue] size = " + actionsQueue.size()
            + " (always should be 0)");
    return action;
}

}

1 Answer 1

2

The ConcurrentLinkedQueue is a wait-free algorithm that may achieve the results you desire:

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.