哪个是多线程java中的队列而没有阻塞

时间:2013-10-27 04:54:35

标签: java multithreading queue

请帮我解决问题。
我正在尝试通过队列将数据从gui线程发送到另一个线程 但我遇到了问题。当另一个线程正在使用队列时,GUI线程将一个对象添加到队列中,Gui线程将被阻塞一些小秒。因此GUI不顺畅 我的班级是:

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 个答案:

答案 0 :(得分:2)

ConcurrentLinkedQueue是一个无等待的算法,可以达到你想要的结果:

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