none
又有问题麻烦各位大侠了,关于并发调用 RRS feed

  • 问题

  •   [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
       public class Service1 : IService1,IDisposable
    {
             public void do()
            {
                System.Threading.Thread.Sleep(3000);

            }
    }

    服务端配置的是percall和multiple,照理是可以同时处理客户端的并发请求。但当我客户端开2个线程调用时,却发现第2个调用被阻塞,直到第1个调用完成后才开始。

    ServiceReference1.IService1 cl = new tranClient.ServiceReference1.Service1Client();

    Thread th = new Thread(new ThreadStart(delegate { cl.geti(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "finish"); }));
    Thread th2 = new Thread(new ThreadStart(delegate { cl.geti(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "finish"); }));

    th.Start();
    th2.Start(); //该线程被阻塞



    而如果我new 2个不同的客户端,即换成如下写法,就发现服务端方法被并发调用了。

     ServiceReference1.IService1 cl = new tranClient.ServiceReference1.Service1Client();
     ServiceReference1.IService1 cl2 = new tranClient.ServiceReference1.Service1Client();

    Thread th = new Thread(new ThreadStart(delegate { cl.geti(); Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "finish"); }));
    Thread th2 = new Thread(new ThreadStart(delegate { cl2.geti() ; Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + "finish"); }));

    th.Start();
    th2.Start(); //2线程可以并发调用



    没有想明白为什么会是这样,同一个客户端的2个线程调用,为什么会被阻塞呢?



    they say nothing last forever
    2009年8月31日 15:37

答案

全部回复