none
.NET 4.0的ThreadPool 设置最大线程数无效。 和 Tasks.Task.Factory.StartNew RRS feed

  • 问题

  • ServicePointManager.DefaultConnectionLimit = 50000
    Threading.ThreadPool.SetMaxThreads(999999999, 999999999)
    Threading.ThreadPool.SetMinThreads(999999999, 999999999)

    最近做一个网页下载 .NET 4.0 只能通过 Thread线程才能实现最大并发连接数的限制
    ThreadPool 和 Tasks.Task.Factory.StartNew HTTP (netstat -an)查看连接数只5~10个

    使用.NET 3.5 用 ThreadPool 查看了N多HTTP连接

    不知道怎么回事?

    ADSL无法看到效果,那位有服务器的测试下不能看是什么问题。。

    Tasks.Task.Factory.StartNew :怎么设置最大线程数? 

     VS 2010  下面代码  ThreadPool Factory HTTP连接数差不多 Threading.Thread才没连接限制。

    在VS 2008 ThreadPool 能实现连接数到100+

     Sub New()
    
      ' 此调用是设计器所必需的。
      InitializeComponent()
    
      ' 在 InitializeComponent() 调用之后添加任何初始化。
      ServicePointManager.DefaultConnectionLimit = 1024
      Threading.ThreadPool.SetMaxThreads(999999999, 999999999)
      Threading.ThreadPool.SetMinThreads(999999999, 999999999)
     End Sub
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
      Dim ta(100) As Task
      Stopw.Start()
      For i As Integer = 0 To 100
       Threading.ThreadPool.QueueUserWorkItem(Sub(id As Integer)
                  Try
                   Using d As New WebClient
                    d.DownloadString("http://www.baidu.com/s?bs=1&f=8&wd=" & id)
                   End Using
                  Catch ex As Exception
    
                  End Try
    
                  Threading.Interlocked.Increment(wc)
                 End Sub, i)
      Next
      While wc <> 100
       Threading.Thread.Sleep(50)
      End While
      Console.WriteLine("QueueUserWorkItem," & String.Format("{0:N5}", Stopw.Elapsed.Ticks / 10000000))
    
      Stopw.Reset()
      wc = 0
      Stopw.Start()
      For i As Integer = 0 To 100
       ta(i) = Task.Factory.StartNew(Sub(id As Integer)
                Try
                 Using d As New WebClient
                  d.DownloadString("http://www.baidu.com/s?bs=1&f=8&wd=" & id)
                 End Using
                Catch ex As Exception
    
                End Try
    
                Threading.Interlocked.Increment(wc)
               End Sub, i)
      Next
      Task.WaitAll(ta)
      Console.WriteLine("Task," & String.Format("{0:N5}", Stopw.Elapsed.Ticks / 10000000))
      Stopw.Reset()
      wc = 0
      Stopw.Start()
      For i As Integer = 0 To 100
       Dim th As New Threading.Thread(Sub(id As Integer)
                Try
                 Using d As New WebClient
                  d.DownloadString("http://www.baidu.com/s?bs=1&f=8&wd=" & id)
                 End Using
                Catch ex As Exception
    
                End Try
                Threading.Interlocked.Increment(wc)
               End Sub, i)
       th.Start()
      Next
      While wc <> 100
       Threading.Thread.Sleep(50)
      End While
      Console.WriteLine("Thread," & String.Format("{0:N5}", Stopw.Elapsed.Ticks / 10000000))
      Stopw.Reset()
      wc = 0
     End Sub
    
    2010年7月7日 8:04

答案

全部回复