这是多线程Java中的队列而没有阻塞 [英] Which is queue in multithread java without blocking
本文介绍了这是多线程Java中的队列而没有阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我解决问题.
我正在尝试通过队列将数据从gui线程发送到另一个线程.
但是我有一个问题.当另一个线程正在使用队列时,GUI线程将一个对象添加到队列中,Gui线程将被阻塞几微秒.因此GUI不流畅.
我的课是:
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;
}
}
推荐答案
ConcurrentLinkedQueue是一种无需等待的算法,可以实现您想要的结果:
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
这篇关于这是多线程Java中的队列而没有阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文