【问题标题】:Which is queue in multithread java without blocking哪个是多线程java中的队列而没有阻塞
【发布时间】:2013-10-27 04:54:35
【问题描述】:

请帮我解决问题。
我正在尝试通过队列将数据从 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;
}

}

【问题讨论】:

    标签: java multithreading queue


    【解决方案1】:

    ConcurrentLinkedQueue 是一种无需等待的算法,可以达到您想要的结果:

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

    【讨论】:

      猜你喜欢
      • 2017-09-13
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 2013-12-13
      • 1970-01-01
      相关资源
      最近更新 更多