0
請幫我解決問題。
我想通過隊列將數據從gui線程發送到另一個線程。
但我遇到了問題。當另一個線程正在使用隊列時,GUI線程將一個對象添加到隊列中,Gui線程將被阻塞一些毫秒。所以GUI不光滑。
我的班級:這是多線程Java中的隊列,無阻塞
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;
}
}