Leo Nguyen
Leo Nguyen

Reputation: 614

Which is queue in multithread java without blocking

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;
}

}

Upvotes: 0

Views: 97

Answers (1)

Jonathan Schneider
Jonathan Schneider

Reputation: 27727

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

Upvotes: 2

Related Questions