将队列与ObjectPool集成 [英] Integrate a Queue with an ObjectPool

查看:60
本文介绍了将队列与ObjectPool集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个有限的对象池(这些对象是昂贵的,因为
create)可以用来处理队列中的项目。此外,我希望
能够在

后台线程中进行处理(耗时)。


我有两个问题:

*在等待免费的

工作人员时阻止队列循环的好方法是什么?

*我的方法是合理的吗?


谢谢,


Craig Buchanan


这里''我的psedo代码:


Sub ProcessQueue


Do While Queue.Count 0


''等待下一个可用的工作人员

Worker = ObjectPool.getFreeWorker()


''将队列中的下一个项目分配给工人

Worker.Item = Queue.Dequeue


''在后台运行耗时的过程

ThreadPool.QueueUserWorkItem(AddressOf ProcessItem,Worker)


循环


结束子


Sub ProcessItem(状态为对象)


Dim WorkerAs Worker = CType(州,工人)


尝试


''流程项目

Worker.Process()


Catch ex As Exception


''将项目退回队列

Queue.Enqueue(Worker.Item)


最后


''让工人回到游泳池

ObjectPool.returnWorker(工人)


结束尝试


结束子

I would like to have a limited pool of objects (the objects are expensive to
create) that can be enlisted to process items in a queue. Moreover, I
would like to be able to have the processing (time-consuming) occur on a
background thread.

I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
* is my approach a reasonable one?

Thanks,

Craig Buchanan

Here''s my psedo-code:

Sub ProcessQueue

Do While Queue.Count 0

''wait for next available worker
Worker = ObjectPool.getFreeWorker()

''assign next item in the queue to the worker
Worker.Item = Queue.Dequeue

''run time-consuming process in background
ThreadPool.QueueUserWorkItem(AddressOf ProcessItem, Worker)

Loop

End Sub

Sub ProcessItem(State As Object)

Dim WorkerAs Worker= CType(State,Worker)

Try

''process item
Worker.Process()

Catch ex As Exception

''return item to queue
Queue.Enqueue(Worker.Item)

Finally

''return worker to pool
ObjectPool.returnWorker(Worker)

End Try

End Sub

推荐答案

我写的这段代码比我算的多了很多次,所以我希望这是一个不错的

模式。


其中一天很快,我会把我的ObjectPool< Tclasses发布到我的博客上,沿着

以及如何使用它们的一些说明。在编写服务器应用程序时,这个问题似乎非常频繁地出现了 - 我们已经有几十个b $ b昂贵的问题。我们维护的对象池。 AD和AD的所有内容LDAP

连接,固定字节[]缓冲区用于读取&写给

套接字(以避免堆碎片)。


它有一个很好的健壮机制来检查&离开泳池

(使用IDisposable以便''使用''块可以使用,并且还使用

对象复活以便运行Finalizer,昂贵的对象

不会迷路)并且它非常易于使用。


等待问题,正如你所描述的那样,我通常会看到它解决了

Monitor.Pulse / PulseAll模式。工作线程被保存在一个

监视器中,当数据被添加到队列时,监视器被脉冲

重新发布其中一个等待线程。你必须小心比赛

条件,但这是一个非常好的模式。


-

Chris Mullins,MCSD.NET,MCPD:Enterprise,Microsoft C#MVP
http://www.coversant.com/blogs/cmullins


" Craig Buchanan" < so ***** @ somewhere.comwrote in message

news:%2 **************** @ TK2MSFTNGP03.phx.gbl .. 。
I''ve written this code more times than I can count, so I hope it''s a decent
pattern to follow.

One of these days soon, I''ll post my ObjectPool<Tclasses to my blog, along
with some instructions on how to use them. This problem seems to come up
very frequently when writing server applications - we''ve got dozens of
"expensive" object pools that we maintain. Everything from AD & LDAP
connections, to pinned byte[] buffers for use in reading & writing to
Sockets (to avoid heap fragmentation).

It''s got a nice robust mechanism for checking objects in & out of the pool
(using IDisposable so that ''using'' blocks can be employed, and also using
Object Resurrection so that the Finalizer is run, the expensive object
doesn''t get lost) and its very easy to use.

You wait problem, as you''ve described it, I usually see it solved with the
Monitor.Pulse / PulseAll pattern. Worker threads are kept blocked in a
Monitor, and when data is added to the queue, the Monitor is Pulsed
realeasing one of the waiting threads. You''ve got to be carefull of race
conditions, but it''s a very well worn pattern.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Craig Buchanan" <so*****@somewhere.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

>我想拥有一个有限的对象池(对象很昂贵,可以创建),这些对象可以被用来处理队列。此外,我希望能够在
后台线程中进行处理(耗时)。


我有两个问题:

*在等待免费的

工作人员时阻止队列循环的好方法是什么?

*我的方法是否合理?


谢谢,


Craig Buchanan


这是我的psedo代码:


Sub ProcessQueue


Do While Queue.Count 0


''等待下一个可用的工人

Worker = ObjectPool.getFreeWorker()


''将队列中的下一项分配给工人

Worker.Item = Queue .Dequeue


''在后台运行耗时的过程

ThreadPool.QueueUserWorkItem(AddressOf ProcessItem,Worker)


循环


结束子


Sub ProcessItem(状态为对象)

Dim WorkerAs禾rker = CType(州,工人)


尝试


''流程项

Worker.Process()


Catch ex As Exception


''将项目返回队列

Queue.Enqueue(Worker.Item)


最后


''让工人回到游泳池

ObjectPool.returnWorker(工人)


结束尝试


结束子
>I would like to have a limited pool of objects (the objects are expensive
to create) that can be enlisted to process items in a queue. Moreover, I
would like to be able to have the processing (time-consuming) occur on a
background thread.

I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
* is my approach a reasonable one?

Thanks,

Craig Buchanan

Here''s my psedo-code:

Sub ProcessQueue

Do While Queue.Count 0

''wait for next available worker
Worker = ObjectPool.getFreeWorker()

''assign next item in the queue to the worker
Worker.Item = Queue.Dequeue

''run time-consuming process in background
ThreadPool.QueueUserWorkItem(AddressOf ProcessItem, Worker)

Loop

End Sub

Sub ProcessItem(State As Object)

Dim WorkerAs Worker= CType(State,Worker)

Try

''process item
Worker.Process()

Catch ex As Exception

''return item to queue
Queue.Enqueue(Worker.Item)

Finally

''return worker to pool
ObjectPool.returnWorker(Worker)

End Try

End Sub



Chris-


感谢您的回复。你的方法是否还使用ThreadPool处理长期工作的工作?


我有机会看到代码吗? craig dot buchanan at cogniza dot com。是你的博客

http://instructors.cwrl.utexas.edu/jesson/?q=blog/61


谢谢,


克雷格


" Chris Mullins [MVP]" < cm ****** @ yahoo.com写信息

新闻:es **************** @ TK2MSFTNGP03.phx.gbl .. 。
Chris-

Thanks for the reply. Does your approach also use the ThreadPool to process
the long-running job?

Any chance I could see the code? craig dot buchanan at cogniza dot com. is
this your blog: http://instructors.cwrl.utexas.edu/jesson/?q=blog/61 ?

Thanks,

Craig

"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
news:es****************@TK2MSFTNGP03.phx.gbl...

我写这段代码的次数比我算的多,所以我希望它是一个好的模式可以跟随它。


其中一天很快,我会将我的ObjectPool< Tclasses发布到我的博客上,

以及一些如何使用它们的说明。在编写服务器应用程序时,这个问题似乎非常频繁地出现 - 我们已经有了几十个昂贵的资金。我们维护的对象池。从AD&

LDAP连接到固定字节[]缓冲区,用于读取和放大写给

套接字(以避免堆碎片)。


它有一个很好的健壮机制来检查&离开泳池

(使用IDisposable以便''使用''块可以使用,并且还使用

对象复活以便运行Finalizer,昂贵的对象

不会迷路)并且它非常易于使用。


等待问题,正如你所描述的那样,我通常会看到它解决了

Monitor.Pulse / PulseAll模式。工作线程被保存在一个

监视器中,当数据被添加到队列时,监视器被脉冲

重新发布其中一个等待线程。你必须小心比赛

条件,但这是一个非常好的模式。


-

Chris Mullins,MCSD.NET,MCPD:Enterprise,Microsoft C#MVP
http://www.coversant.com/blogs/cmullins


" Craig Buchanan" < so ***** @ somewhere.comwrote in message

news:%2 **************** @ TK2MSFTNGP03.phx.gbl .. 。
I''ve written this code more times than I can count, so I hope it''s a
decent pattern to follow.

One of these days soon, I''ll post my ObjectPool<Tclasses to my blog,
along with some instructions on how to use them. This problem seems to
come up very frequently when writing server applications - we''ve got
dozens of "expensive" object pools that we maintain. Everything from AD &
LDAP connections, to pinned byte[] buffers for use in reading & writing to
Sockets (to avoid heap fragmentation).

It''s got a nice robust mechanism for checking objects in & out of the pool
(using IDisposable so that ''using'' blocks can be employed, and also using
Object Resurrection so that the Finalizer is run, the expensive object
doesn''t get lost) and its very easy to use.

You wait problem, as you''ve described it, I usually see it solved with the
Monitor.Pulse / PulseAll pattern. Worker threads are kept blocked in a
Monitor, and when data is added to the queue, the Monitor is Pulsed
realeasing one of the waiting threads. You''ve got to be carefull of race
conditions, but it''s a very well worn pattern.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Craig Buchanan" <so*****@somewhere.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

>>我想拥有一个有限的对象池(对象很昂贵,可以创建),可以用来处理项目在队列中。此外,我希望能够在
后台线程中进行处理(耗时)。

我有两个问题:
* what在等待免费的工作人员时阻止队列循环的好方法是什么?
*我的方法是合理的吗?

谢谢,
Craig Buchanan

这是我的psedo代码:

Sub ProcessQueue

Do While Queue.Count 0

''等待下一个可用的工作人员
Worker = ObjectPool.getFreeWorker()
''将队列中的下一个项目分配给工作人员
Worker.Item = Queue.Dequeue

''在后台运行耗时的过程
ThreadPool.QueueUserWorkItem(AddressOf ProcessItem,Worker)

循环

End Sub

Sub ProcessItem(状态为对象)

Dim WorkerAs Worker = CType(州,工人)

尝试

' '流程项目
Worker.Process()

Catch ex As Exception

''将项目返回队列
Queue.Enqueue(Worker.Item)

最后

返回工作人员
ObjectPool.returnWorker(工人)

结束尝试

结束子
>>I would like to have a limited pool of objects (the objects are expensive
to create) that can be enlisted to process items in a queue. Moreover, I
would like to be able to have the processing (time-consuming) occur on a
background thread.

I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
* is my approach a reasonable one?

Thanks,

Craig Buchanan

Here''s my psedo-code:

Sub ProcessQueue

Do While Queue.Count 0

''wait for next available worker
Worker = ObjectPool.getFreeWorker()

''assign next item in the queue to the worker
Worker.Item = Queue.Dequeue

''run time-consuming process in background
ThreadPool.QueueUserWorkItem(AddressOf ProcessItem, Worker)

Loop

End Sub

Sub ProcessItem(State As Object)

Dim WorkerAs Worker= CType(State,Worker)

Try

''process item
Worker.Process()

Catch ex As Exception

''return item to queue
Queue.Enqueue(Worker.Item)

Finally

''return worker to pool
ObjectPool.returnWorker(Worker)

End Try

End Sub




Chris-


我想知道ObjectPool驱动这个过程是否有意义

'排空'队列。想想银行出纳员如何处理一行

的客户。


也许会有一个事件(或代表)会在项目池中的
变为免费。在活动的代码中,工作人员下次会在队列中获取




好​​像需要有一个方法ObjectPool表示这个

进程开始。也许它可以听取由

queue.enqueue()引发的事件。


想法?


" ; Chris Mullins [MVP]" < cm ****** @ yahoo.com写信息

新闻:es **************** @ TK2MSFTNGP03.phx.gbl .. 。
Chris-

I''m wondering if it would make sense for the ObjectPool to drive the process
of ''draining'' the queue. Think of how bank tellers process a line of
customers.

Perhaps there would be an event (or delegate) that would fire when an item
in the pool becomes free. in the event''s code, the worker would grab the
next time in the queue.

Seems like there would need to be a method on the ObjectPool to signal this
process to begin. Perhaps it could listen to an event raised by
queue.enqueue().

Thoughts?

"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
news:es****************@TK2MSFTNGP03.phx.gbl...

我写这段代码的次数比我算的多,所以我希望它是一个好的模式可以跟随它。


其中一天很快,我会将我的ObjectPool< Tclasses发布到我的博客上,

以及一些如何使用它们的说明。在编写服务器应用程序时,这个问题似乎非常频繁地出现 - 我们已经有了几十个昂贵的资金。我们维护的对象池。从AD&

LDAP连接到固定字节[]缓冲区,用于读取和放大写给

套接字(以避免堆碎片)。


它有一个很好的健壮机制来检查&离开泳池

(使用IDisposable以便''使用''块可以使用,并且还使用

对象复活以便运行Finalizer,昂贵的对象

不会迷路)并且它非常易于使用。


等待问题,正如你所描述的那样,我通常会看到它解决了

Monitor.Pulse / PulseAll模式。工作线程被保存在一个

监视器中,当数据被添加到队列时,监视器被脉冲

重新发布其中一个等待线程。你必须小心比赛

条件,但这是一个非常好的模式。


-

Chris Mullins,MCSD.NET,MCPD:Enterprise,Microsoft C#MVP
http://www.coversant.com/blogs/cmullins


" Craig Buchanan" < so ***** @ somewhere.comwrote in message

news:%2 **************** @ TK2MSFTNGP03.phx.gbl .. 。
I''ve written this code more times than I can count, so I hope it''s a
decent pattern to follow.

One of these days soon, I''ll post my ObjectPool<Tclasses to my blog,
along with some instructions on how to use them. This problem seems to
come up very frequently when writing server applications - we''ve got
dozens of "expensive" object pools that we maintain. Everything from AD &
LDAP connections, to pinned byte[] buffers for use in reading & writing to
Sockets (to avoid heap fragmentation).

It''s got a nice robust mechanism for checking objects in & out of the pool
(using IDisposable so that ''using'' blocks can be employed, and also using
Object Resurrection so that the Finalizer is run, the expensive object
doesn''t get lost) and its very easy to use.

You wait problem, as you''ve described it, I usually see it solved with the
Monitor.Pulse / PulseAll pattern. Worker threads are kept blocked in a
Monitor, and when data is added to the queue, the Monitor is Pulsed
realeasing one of the waiting threads. You''ve got to be carefull of race
conditions, but it''s a very well worn pattern.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Craig Buchanan" <so*****@somewhere.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

>>我想拥有一个有限的对象池(对象很昂贵,可以创建),可以用来处理项目在队列中。此外,我希望能够在
后台线程中进行处理(耗时)。

我有两个问题:
* what在等待免费的工作人员时阻止队列循环的好方法是什么?
*我的方法是合理的吗?

谢谢,
Craig Buchanan

这是我的psedo代码:

Sub ProcessQueue

Do While Queue.Count 0

''等待下一个可用的工作人员
Worker = ObjectPool.getFreeWorker()
''将队列中的下一个项目分配给工作人员
Worker.Item = Queue.Dequeue

''在后台运行耗时的过程
ThreadPool.QueueUserWorkItem(AddressOf ProcessItem,Worker)

循环

End Sub

Sub ProcessItem(状态为对象)

Dim WorkerAs Worker = CType(州,工人)

尝试

' '流程项目
Worker.Process()

Catch ex As Exception

''将项目返回队列
Queue.Enqueue(Worker.Item)

最后

返回工作人员
ObjectPool.returnWorker(工人)

结束尝试

结束子
>>I would like to have a limited pool of objects (the objects are expensive
to create) that can be enlisted to process items in a queue. Moreover, I
would like to be able to have the processing (time-consuming) occur on a
background thread.

I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
* is my approach a reasonable one?

Thanks,

Craig Buchanan

Here''s my psedo-code:

Sub ProcessQueue

Do While Queue.Count 0

''wait for next available worker
Worker = ObjectPool.getFreeWorker()

''assign next item in the queue to the worker
Worker.Item = Queue.Dequeue

''run time-consuming process in background
ThreadPool.QueueUserWorkItem(AddressOf ProcessItem, Worker)

Loop

End Sub

Sub ProcessItem(State As Object)

Dim WorkerAs Worker= CType(State,Worker)

Try

''process item
Worker.Process()

Catch ex As Exception

''return item to queue
Queue.Enqueue(Worker.Item)

Finally

''return worker to pool
ObjectPool.returnWorker(Worker)

End Try

End Sub




这篇关于将队列与ObjectPool集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆