TCP多线程还没多少经验,希望大家能帮我找找我的问题出在哪儿。谢谢大家了
解决方案 »
- datalist问题 没分了
- 求教各位高手一个虚函数的问题,实在搞不懂??
- C#委托是如何实现的?
- C SHARP制作类似QQ聊天框的RichTextBox的自动增删文本功能,遇到一些问题。进来看看。。
- 有关命名空间的理解
- this.dataadapter1.Fill(this.dataset, "stu"); 这句代码时候 说 我要表 已经被独占打开?怎么回事?在线 ……等待
- 打印dataGrid中选定的行,最好用水晶报表???????????????
- 急!
- 入门问题
- SqlServer 新增出錯,提示:the 'PRIMARY' filegroup is full.
- 按钮触发新页面及生成xml
- vs.net平台,使用c#调用borland c++6.0开发com组件
http://www.cnblogs.com/surfsky/archive/2008/12/12/1353437.html
呵呵,虚拟内存1.7g。32位系统的4gb上限。我还是不太相信会有人用1800个线程去跑。你确信?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Collections.Generic;namespace ObjectPoolConsole
{
class BufferPool
{
private ConcurrentStack<byte[]> _stack;
private int _size;
private int _allocationCount; public BufferPool(int size)
{
_size = size;
_stack = new ConcurrentStack<byte[]>();
} public int Size
{
get { return _size; }
} public int AllocationCount
{
get { return _allocationCount; }
} public int Count
{
get { return _stack.Count; }
} public byte[] Take()
{
byte[] result;
if (_stack.TryPop(out result))
{
return result;
}
//池分配
System.Threading.Interlocked.Add(ref _allocationCount, 1);
return new byte[_size];
} public void Return(byte[] buffer)
{
_stack.Push(buffer);
}
} class BufferManager
{
private BufferPool[] _pools;
private int[] _poolSizes;
public BufferManager(int maxBufferSize)
{
System.Diagnostics.Debug.Assert(maxBufferSize >= 128); //最小分配大小为128个字节。
long bufferSize = 128;
var list = new List<BufferPool>();
while (bufferSize < maxBufferSize)
{
list.Add(new BufferPool((int)bufferSize));
bufferSize *= 2L;
}
list.Add(new BufferPool(maxBufferSize)); _pools = list.ToArray();
_poolSizes = new int[_pools.Length];
for (int i = 0; i < _pools.Length; ++i) {
_poolSizes[i] = _pools[i].Size;
}
} public int PoolBufferCount
{
get
{
int result = 0;
foreach (var pool in _pools)
{
result += pool.Count;
}
return result;
}
} public int PoolAllocationCount
{
get
{
int result = 0;
foreach (var pool in _pools)
{
result += pool.AllocationCount;
}
return result;
}
} public byte[] TakeBuffer(int size)
{
BufferPool pool = FindBufferPool(size);
if (pool != null)
{
pool.Take();
}
//大对象直接分配。
return new byte[size];
} public void ReturnBuffer(byte[] buffer)
{
BufferPool pool = FindBufferPool(buffer.Length);
if (pool != null)
{
pool.Return(buffer);
}
} private BufferPool FindBufferPool(int bufferSize)
{
for (int i = 0; i < _poolSizes.Length; ++i)
{
if(bufferSize <= _poolSizes[i])
{
return _pools[i];
}
}
return null;
}
}
}
测试代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace ObjectPoolConsole
{
class Program
{
static BufferManager manager = new BufferManager(1024); static void Main(string[] args)
{ List<Thread> list = new List<Thread>();
for (int i = 0; i < 1000; i++)
{
Thread t = new Thread(AllocBuffer);
list.Add(t);
t.Start();
} foreach (Thread t in list)
{
t.Join();
} Console.WriteLine("创建Buffer的个数为:{0}", manager.PoolBufferCount);
Console.WriteLine("分配Buffer的次数为:{0}", manager.PoolAllocationCount);
Console.ReadLine();
} static void AllocBuffer() {
Random r = new Random();
int size = r.Next(1024);
var buffer = manager.TakeBuffer(size);
r.NextBytes(buffer);
Thread.Sleep(1000);
manager.ReturnBuffer(buffer);
}
}
}这里提供了一个简单的Buffer池框架。很多控制方面的东西都没加。