我正在寻找与 Groovy 语言中的 Python 中的队列类型类似的东西。
在Python中,队列可以在线程之间交换信息并处理所有锁定问题/挑战。因此一个线程可以简单地向队列添加一个任务
while True:
if self.task_ready():
task = self.get_task()
self.queue.put(task)
print 'Task %s added to queue' % (task.name)
工作线程可以:
while True:
self.queue.get() # this is read blocking action
self.proces_task(task)
在 Groovy 中有没有办法以如此简单的方式做到这一点?
最佳答案
您可以使用队列。
Java有LinkedBlockingQueue在其 java.util.concurrent 包中,您可以从 Groovy 中使用它。有一篇文章over here on JavaLobby讨论这些结构(显然是从 Java 角度来看的,但您可以使用 Groovy 中的所有内容)
关于groovy 中的 Python 队列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11976409/