假设我有一堂课
XYNode
{
protected int mX;
protected int mY;
}
Run Code Online (Sandbox Code Playgroud)
和一个队列
Queue<XyNode> testQueue = new Queue<XYNode>();
Run Code Online (Sandbox Code Playgroud)
我想检查具有该特定x和y坐标的节点是否已在队列中.以下显然不起作用:
testQueue.Contains(new XYNode(testX, testY))
Run Code Online (Sandbox Code Playgroud)
因为即使具有这些坐标的节点在队列中,我们也会针对不同的XYNode对象进行测试,因此它总是返回false.
什么是正确的解决方案?
我有一个队列,我可以在其中排队不同的线程,所以我可以保证两件事:
第二点很重要.否则,一个简单的关键部分就足够了.我有不同的请求组,只有在一个组内才能满足这些要求.来自不同组的请求可以并发运行.
它看起来像这样:
FTaskQueue.Enqueu('MyGroup');
try
Do Something (running in context of some thread)
finally
FTaskQueue.Dequeu('MyGroup');
end;
Run Code Online (Sandbox Code Playgroud)
编辑:我删除了实际的实现,因为它隐藏了我想要解决的问题
我需要这个,因为我有一个基于Indy的Web服务器接受http请求.首先,我找到了请求的相应会话.然后为该会话执行请求(代码).我可以获得同一会话的多个请求(读取我可以获得新请求,而第一个仍处理)并且它们必须以正确的到达顺序逐个执行.所以我寻找一个可以在这种情况下使用的通用同步队列,以便请求可以排队.我无法控制线程,每个请求可以在不同的线程中执行.
这种问题的最佳(使用)方法是什么?问题是Enqueue和Dequeue必须是原子操作,以便保存正确的顺序.我目前的实施有很大的瓶颈,但它确实有效.
编辑:Bellow是原子Enqueue/Dequeue操作的问题
你正常地做这样的事情:
procedure Enqueue;
begin
EnterCriticalSection(FCritSec);
try
DoEnqueue;
finally
LeaveCriticalSection(FCritSec);
end;
BlockTheCurrentThread; // here the thread blocks itself
end;
procedure Dequeue;
begin
EnterCriticalSection(FCritSec);
try
DoDequeue;
UnblockTheNextThread; // here the thread unblocks another thread
finally
LeaveCriticalSection(FCritSec);
end;
end;
Run Code Online (Sandbox Code Playgroud)
现在问题在于这不是原子的.如果队列中已有一个线程而另一个线程调用Enqueue,则可能会发生第二个线程将离开临界区并尝试阻塞自身.现在线程调度程序将恢复第一个线程,它将尝试取消阻塞下一个(第二个)线程.但是第二个线程还没有被阻止,所以没有任何反应.现在第二个线程继续并阻塞自身,但这不正确,因为它不会被解除阻塞.如果阻塞在临界区内,那么临界区永远不会离开,我们就会陷入僵局.
以下是我创建的测试用例.为什么每个进程都打印1到5的数字,并且这些数字是不分为进程的数字?
码:
#!/usr/bin/python
from subprocess import *
from Queue import Queue
from Queue import Empty
import multiprocessing
from multiprocessing import Process
def main():
r = Runner()
r.run()
class Runner(object):
processes = []
def run(self):
q = Queue()
for t in range(1,6):
q.put(t)
for pi in range(1,4):
p = Process(target=self.runFromQueue, args=(q,))
p.start()
self.processes.append(p)
for p in self.processes:
p.join()
print "Finished!"
def runFromQueue(self, q):
try:
while True:
number = q.get_nowait()
print str(number)
q.task_done()
except Empty:
pass
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
输出继电器: …
我有一个WorkerThread工作在队列中的元素和几个MiningThread创建东西的东西需要由WorkerThread.
总结:一个线程Dequeues和几个Enqueue.
我是否需要使用msdn中提出的同步模式,或者在此特定方案中我是线程安全的?
从msdn一个简单的同步访问模式
Queue myCollection = new Queue();
lock(myCollection.SyncRoot)
{
foreach (object item in myCollection)
{
// Insert your code here.
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为队列类型创建一个包装器,但每次添加元素时,我都要对内部的所有内容进行排序.大多数情况下它将是整数.我对Collections框架不太熟悉,有什么简单的解决方案吗?
public class Round<Type> {
private Queue<Type> qe;
public Round(){
this.qe = new LinkedList<Type>();
}
public void push(Type p){
this.qe.offer(p);
//Collections.sort(this.qe); Here I want to sort this
}
public Type pop(){
return this.qe.poll();
}
}
Run Code Online (Sandbox Code Playgroud) 是否有可能实现一个完全无等待并且可以同时支持多个编写器和读取器的并发队列?我知道我可以使用互斥锁或现有的库,但我真的想自己实现它.任何想法?
jQuery队列很烦人 - 我无法理解它...
以下函数包含4个动画,我希望一个接一个地发生(不是同时发生).
function startupAnime() {
$("#header").animate({"opacity":"1"},{duration:3000, queue:"global"});
$("#header").animate({"top":"0px"},{duration:3000, queue:"global"});
&("#loader").animate({"opacity":"0"},{duration:3000, queue:"global"});
$("#background").animate({"background-position" : "300px center"},{duration:3000, queue:"global"});
}
$(window).load(function() {
startupAnime();
})
Run Code Online (Sandbox Code Playgroud)
两个#header元素将一个接一个地动画,但其余的都在同一时间发生.
然后我发现Queue只有在你动画相同的元素时才有效!! 这不是很有用......但是添加队列:"名称"显然应该将它们链接到同一个队列中......虽然这只会阻止他们为我工作.
我在这里错过了什么吗?我不确定队列是否意味着"下一个动画将在当前一个动画完成后开始",或者"这些动画被保存在一个等待你释放它们的队列中"可能是通过调用队列("全局")或其他东西!
大多数建议都讨论动画一个元素,或设置许多函数,并使用回调来"迭代"函数 - 如果你问我,不是很干净.我只是希望能够在我要求时运行动画列表.
注意:动画的"列表"可以一次为一个元素设置动画,但我也可能希望两个或更多元素在列表中的某个点同时进行动画处理.这是一个示例:
animate element A
-> then ->
animate element B
-> then ->
animate element C and D together
-> then ->
animate element E
Run Code Online (Sandbox Code Playgroud) 所以我有一个队列:
q = Queue.Queue()
Run Code Online (Sandbox Code Playgroud)
我在里面放一些东西。
items = ["First", "Second"]
for val in items:
q.put(val)
Run Code Online (Sandbox Code Playgroud)
我正在生成15个线程。
for i in range(15):
tname = 't-%s' % i
t = my_thread(some_func, q, tname)
t.start()
q.join()
Run Code Online (Sandbox Code Playgroud)
my_thread类的外观如下:
class my_thread(threading.Thread):
def __init__(self, some_func, q_, name=''):
threading.Thread.__init__(self)
self.func = some_func
self.process_q = q_
self.name = name
self.prefix = name
def run(self):
stime = time.time()
logging.info('%s thread staring at : %s' % (threading.currentThread().getname(), time.ctime(stime)))
while True:
if self.process_q.empty():
break
queue_item = self.process_q.get()
self.name = self.prefix + '-' + …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现基本的多处理,但是遇到了一个问题。python脚本附在下面。
import time, sys, random, threading
from multiprocessing import Process
from Queue import Queue
from FrequencyAnalysis import FrequencyStore, AnalyzeFrequency
append_queue = Queue(10)
database = FrequencyStore()
def add_to_append_queue(_list):
append_queue.put(_list)
def process_append_queue():
while True:
item = append_queue.get()
database.append(item)
print("Appended to database in %.4f seconds" % database.append_time)
append_queue.task_done()
return
def main():
database.load_db()
print("Database loaded in %.4f seconds" % database.load_time)
append_queue_process = Process(target=process_append_queue)
append_queue_process.daemon = True
append_queue_process.start()
#t = threading.Thread(target=process_append_queue)
#t.daemon = True
#t.start()
while True:
path = raw_input("file: ")
if path == "exit": …Run Code Online (Sandbox Code Playgroud) 如何检查在threadpool.index.queue_size和threadpool.bulk.queue_size中定义的ealastisearch队列的当前大小?
SO上已经有一些与Elasticsearch中的排队问题相关的主题/问题:ElasticSearch给出了有关队列大小的错误
这是关于如何设置队列大小。但是如何读取这些队列上的当前(实时)大小/负载,以避免在运行时使Elasticsearch过载,或者如何查看它们是否得到了最佳使用以及长度是否正确设置?
我试图在CAT API中找到答案,但是与这些队列没有任何明确的关联(也许我什么都没看到)。
queue ×10
python ×3
c# ×2
.net-3.5 ×1
c++ ×1
collections ×1
concurrency ×1
database ×1
delphi ×1
futex ×1
java ×1
jquery ×1
nonblocking ×1
reference ×1
size ×1
synchronous ×1