Which is queue in multithread java without blocking

情到浓时终转凉″ 提交于 2019-12-11 21:26:00

问题


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:


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



来源:https://stackoverflow.com/questions/19614752/which-is-queue-in-multithread-java-without-blocking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!