We have a general understanding of the behavior patterns of several chat rooms
The simplest and most straightforward push mode requires almost no extra language to describe its implementation.
This article we see how to achieve the pull mode is more effective.
This diagram clearly shows the behavior of the "pull" mode chat room.
Concurrent multiuser writes data to data pool
Concurrent multiuser reading from data pool
Data is best stored in the collection in chronological order
An enumeration lookup that is backward at some time will be the most consumed.
Chat room Evolution-Maid chronicle of the Mysterious Primitive Society
Still refer to our magical simplicity of Asp3 chat room
53 Application.lock
54 Application("show5")=Application("show4") '一条新信息 驾到 第五条信息被淘汰
55 Application("show4")=Application("show3")
56 Application("show3")=Application("show2")
57 Application("show2")=Application("show")
58 Application("show")=NewMessage '其他所有的信息向前移 动一次给新的信息让个位置。
59 Application.UnLock
60 Response.Write Application ("show5")
61 Response.Write Application("show4") '由于 是postback 模式 必须输出历史n行数据
62 Response.Write Application ("show3")
63 Response.Write Application("show2")
64 Response.Write Application("show")
From a thread-safe standpoint, Response.Write should also be in the application. Lock block or separate two lock blocks. But here, due to the delay in Response.Write mode, the authors have painstakingly moved them out of the security lock. There is a good chance of missing or repeating speeches in actual operation.
What the hell did application do? No boundaries, no abstract packaging. This realization is like the original communism who is who who ah this is all!
Private ownership appears, slave society lock~ This slave is mine ~
Translated into C # we can see a more understandable logic of course, this code is slightly modified. The two locks are clear and perfect. Line up the data and threads
Code Snippet
class Channel
{
Queue<string> MessageQ = new Queue<string>();
public void Say(string message) //写信息
{
lock (MessageQ)
{
MessageQ.Enqueue(message);
while (MessageQ.Count > 5) // 删多余
{
MessageQ.Dequeue();
}
}
}
public string[] Listen() //u-28781 ?出所有
{
lock (MessageQ)
{
return MessageQ.ToArray();
}
}
}