请帮我解决问题。
我正在尝试通过队列将数据从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;
}
}
答案 0 :(得分:2)
ConcurrentLinkedQueue是一个无等待的算法,可以达到你想要的结果:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html