自己设计的一个基于Udp通信协议的RPC机制,在client向Server发送某个逻辑的请求后,等待server的返回,这个时间段client该怎么做?我目前的做法是:
传送的数据:
1,请求,请求相关数据和交易号
2,返回,返回数据和交易号
维护两个map表:
1,交易号和线程句柄
2,交易号和返回数据请求:
1,把交易号和请求相关数据发送给server
2,把交易号和当前线程句柄放到map里面去
3,调用Thread.Suspend()方法
返回:
1,server启动一个线程来处理请求,返回结果
2,client接收到返回后,把交易号和返回数据的放到map里面
3,依据交易号去线程那个map里面把相应的thread.rusmue起来
4,client便可以继续执行,拿交易号去返回数据的map里面把返回数据取出.因为刚开始用C#才十几天,所以不知道很多比较适当的做法.上面的做法在网络通信保障的情况下可以,只是现在有两个问题,1是本身的性能问题2是要加一个重发到另外的server上的功能.我看到资料说尽量不要用suspend和resume方法,可以用sleep和interrupt,然后在异常里面去取返回数据,这样做会更好吗?
重发的问题是在请求发出后,一段时间里面,如果没有收到回应,则把请求数据发到另外一个server上,我开始想用在请求后同时起一个线程来计时,并把当前线程句柄传给计时的线程,如果到时间线程没有被resume,则主动resume并重发,觉得成本太大,放弃了.后来看到spinwait方法,不知道可不可以用,没用过.还是倾向用sleep和interrupt.大家有什么好的建议吗?其实实现倒是没有问题,我就担心成本大,没有什么C#的经验最怕的就是写出成本很大的代码.特别是读到下面这篇文章,更是有点担心:<引用>
4.4 多线程 Multithreading 
1. 应用同步机制空间。 
Use Synchronization Domains. See Chapter 8 in Programming .NET Components. 
避免手工的同步机制,应为这样容易导致死锁和竞态条件。 
a) Avoid manual synchronization because that often leads to deadlocks and race conditions. 
2. 永远不要在外部调用你的同步机制空间。 
Never call outside your synchronization domain. 
3. 用回调方法控制非同步调用 
Manage asynchronous call completion on a callback method. 
不要等待,调查 或者阻碍完成。 
a) Do not wait, poll, or block for completion. 
4. 总是命名线程。 
Always name your threads. 
线程名字可以在线程调试窗体里跟踪,所以做一个调试调试线程会使程序变得更有效。 
a) Name is traced in the debugger Threads window, making a debug session more productive. 
Thread currentThread = Thread.CurrentThread; 
String threadName = “Main UI Thread”; 
currentThread.Name = threadName; 
5. 不要在线程中调用Suspend() 或者 Resume() 
Do not call Suspend() or Resume() on a thread. 
6. 不要调用Thread.Sleep() 
Do not call Thread.Sleep(). 
a) Thread.Sleep(0) is acceptable optimization technique to force a context switch. 
b) Thread.Sleep() is acceptable in testing or simulation code. 
7. 不要调用Thread.SpinWait() 
Do not call Thread.SpinWait(). 
8. 不要调用Thread.Abort()来结束线程。 
Do not call Thread.Abort() to terminate threads. 
不是标记线程的结束,而是应用同步对象去完成。 
a) Use a synchronization object instead to signal the thread to terminate. See Chapter 8 in Programming .NET Components. 
9. 避免显示地设定控制执行的优先权。 
Avoid explicitly setting thread priority to control execution. 
可以基于任务来设定优先权,例如用于屏幕存储器的线程应该低于一般水平。 
a) Can set thread priority based on task semantic, such as below normal for a screen saver. 
10. 不要读取ThreadState属性的value值。 
Do not read the value of the ThreadState property. 
应用方法Thread.IsAlive()来判断线程是否失效。 
a) Use Thread.IsAlive() to determine whether the thread is dead. 
11. 应用程序结束时,不要通过设置线程类型来设定线程。 
Do not rely on setting the thread type to background thread for application shutdown. 
可以用watchdog或其他监控工具来坚决的结束线程。 
a) Use a watchdog or other monitoring entity to deterministically kill threads. 
12. 不要应用线程的本地存储,除非该线程的相关性已经得到保证。 
Do not use thread local storage unless thread affinity is guaranteed. 
13. 不要调用Thread.MemoryBarrier()。 
Do not call Thread.MemoryBarrier(). 
14. 在未做检验之前万不可调用Thread.Join()。 
Never call Thread.Join() without checking that you are not joining your own thread. 
Void WaitForThreadToDie(Thread thread) 

Debug.Assert(Thread.CurrentThread.GetHashCode() != thread.GetHashCode()); 
Thread.Join(); 

15. 总是应用lock()声明,而不是用明显的Monitor manipulation。 
Always use the lock() statement rather than explicit Monitor manipulation. 
16. 总是将lock()声明放在它所保护的对象里面。 
Always encapsulate the lock() statement inside the object it protects. 
Public class MyClass 

public void DoSomething() 

lock(this) 
{…} 


a)可以使用同步方法来代替你自己写lock()声明。 
Can use synchronized methods instead of writing the lock() statement yourself. 
17. 避免分散锁定。 
Avoid fragmented locking(see Chapter 8 of Programming .NET Components). 
18. 避免用监视器去等待或刺激对象。应该用手工的或自动重起事件。 
Avoid using a Monitor to wait or pulse objects. Use manual or auto-reset events instead. 
19. 不要使用不稳定变量。锁定对象或域,而不是去保证决定性和线程安全访问。 
Do not use volatile variables. Lock your object or fields instead to guarantee deterministic and thread-safe access. 
不要使用Thread.VolatileRead(), Thread.VolatileWrite()或者不稳定修改器。 
a) Do not use Thread.VolatileRead(), Thread.VolatileWrite() or the volatile modifier. 
20. 永远不要stack声明lock,因为这样不提供自动锁定。要使用WaitHandle.WaitAll()。 
Never stack lock statements because that does not provide automatic locking. Using WaitHandle.WaitAll() instead. 
MyClass obj1 = new MyClass(); 
MyClass obj2 = new MyClass(); 
MyClass obj3 = new MyClass(); //Do not stack lock statements 
lock(obj1) 
lock(obj2) 
lock(obj3) 

obj1.DoSomething(); 
obj1.DoSomething(); 
obj1.DoSomething(); 

</引用>大家给给建议丫,非常谢谢.