[ASK]-关于线程 [英] [ASK] - About Thread

查看:87
本文介绍了[ASK]-关于线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家们,我想问问您有关线程的问题.我在执行绪时遇到问题,我的网路服务包含3种方法,例如:ReceiveDataStatic,ReceiveDataInvestor和ReceiveDataValidator.

当客户端要使用此服务时,他们可以同时使用这些方法.我对我的应用程序的性能感到困惑,所以我做了一些类似这样的线程::〜


Hi experts, i wanna ask u about thread. i''ve a problem in threading, i''ve had webservice that contain 3 method, such as : ReceiveDataStatic, ReceiveDataInvestor, and ReceiveDataValidator.

When the client want to use this service, they can use these methods at the same time. I''ve confused about the performance of my application,so i''ve made some thread like this : :~


// This is the web method
[WebMethod]
public void ReceiveDataStatic(object obj)
{
  ClassMessage o = new ClassMessage();
  o.ProcessMessage(obj); // Call ProcessMessage method in other class
}

[WebMethod]
public void ReceiveDataInvestor(object obj)
{
   ClassMessage o = new ClassMessage();
   o.ProcessMessage(obj);
}

[WebMethod]
public void ValidatorDataInvestor(object obj)
{
   ClassMessage o = new ClassMessage();
   o.ProcessMessage(obj);
}









//Class ClassMessage.cs
public void ProcessMessage(object obj)
{
  switch (type)
  {
    case "Data Static":
      oThread1 = new Thread(ParseDataStatic);
      oThread1.Start(obj);
      break;
    case "Data Investor":
      oThread2 = new Thread(ParseDataInvestor);
      oThread2.Start(obj);
      break;
    case "Inventory Validation":
      oThread3 = new Thread(ParseDataInvValidation);
      oThread3.Start(obj);
      break;
  }


}

嗯..我想,这是我的把戏..:~~我们知道,要处理的事情是基于FIFO(先进先出)的.因此,如果一个客户端要首先使用"ReceiveDataStatic",则将尽早处理此方法公开的消息.以及,如果其他客户端使用其他方法,mmm ..我们假设他们同时使用"ReceiveDataInvestor"方法呢?这个线程可以同时运行吗(我们必须记住关于FIFO)? (我的意思是2个线程可以同时运行.)

有什么建议或解决此问题的最佳方法吗?请帮助我..:confused:


}

Hhhmmm.. i think, this my trick.. :~ As we know, to process something is based on FIFO (First In, First Out). So, if one client want to use "ReceiveDataStatic" first, the message that is reveived by this method will be processed as early as possible. And how, if other client using other method, mmm.. we assume they use "ReceiveDataInvestor" method at the same time?? Is this thread can run at the same time (we must remember about FIFO)?? (i mean that 2 threads can run simultaneously.)

Is there any suggestion or the best way for this problem..?? help me please.. :confused:

推荐答案

您的编码有一些基本问题.您不能真正从Web方法中产生新线程并有效利用它.

当请求到达Asp.net应用程序时,Asp.net运行时从线程池中选择一个线程来满足此特定请求,并且一旦满足该请求,该线程就会死亡.在内部,Web服务方法也是Web请求,因此也从线程池中提取了一个线程来执行Web方法,并且一旦方法调用完成,线程就照常死掉.

现在,如果您从Web服务方法中生成一个或多个子线程,则不能保证这些线程可以完成其工作,因为父线程(正在处理当前请求的线程)可能已经死亡.如果父线程死亡,则子线程将立即死亡,而无需完成其工作.

因此,您实际上不应从Web服务方法中使用线程:)
Your coding has some fundamental problem. You cannot really spawn new threads from a web method and utilize that effectively.

When a request arrives at an Asp.net application, a single thread is picked by the Asp.net runtime from the thread pool to serve this particular request and once the request is served, the thread dies. Internally, a web service method is also a web request and hence a single Thread is also taken from the thread pool to execute the web method and once the method invocation is done, the thread dies as usual.

Now, if you spawn one or more child threads from within the web service method, it''s not guaranteed that these threads can complete their work because the parent thread (The thread that is serving the current request) may die already. If the parent thread dies, the child threads will be dead immediately without finishing their work.

So, you actually shouldn''t use threading from within a Web service method :)


您应该尝试尽可能地优化处理.别无选择:)

祝你好运.
You should try to optimize your processing as much as possible. No alternative :)

Best of luck.


这篇关于[ASK]-关于线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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