线程池数量过多,最多200个线程! [英] threadpool count too much up to 200 threads!

查看:445
本文介绍了线程池数量过多,最多200个线程!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码,我在10,000个队列中运行,但是线程池计数很高.

谁能帮助我找出错误的答案



i have this code which i run with queued 10,000, but the threadpool counts goes to high.

can anyone help me fish out the fault



Public Sub DoWork(ByVal objItem As Object)
		static  _sem as Semaphore  = new Semaphore(5,10)    ''// Capacity of 10
		Dim objUrl as String = DirectCast(objItem, string)
		Try
			If objUrl Is Nothing Then
				exit sub
			End If
			
			Dim URL as New Uri(objUrl)
			AddItem(URL.AbsoluteUri,gethtml(URL))
		Catch ex As Exception
			debug.Print("Error " & ex.StackTrace )
		End Try
		
	End Sub
	
	Private Sub btnStart_Click(ByVal sender As System.Object, _
		ByVal e As System.EventArgs) Handles btnStart.Click
		btnStart.Enabled =False
		ThreadPool.SetMaxThreads(50, 50)
		System.Threading.Thread.Sleep(1000)
		listView1.Items.Clear
		
		For Each sItem As String In txturls.Lines
			if sItem.Trim <>string.empty
				'' Queue a task
				If sItem.Contains("http://")=False Then
					sItem="http://" & sItem
				End If
				
				ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf DoWork), sItem)
				TotalItems+=1
				tsslTotal.Text =String.Format ("Total Tasks: {0}",TotalItems)
				
			end if
		Next
		
	End Sub



Thanks



thanks

推荐答案

即使使用线程池,在某个UI事件上生成新线程的整个想法也是错误的.您可以轻松耗尽系统,而无需任何使用.如果您在逻辑上有不同的过程要并行运行,则需要线程.想一想:如果您一次只做相同的事情,您将获得什么并行性?也许,这只是实验,然后我会明白的.在现实生活中,您通常只使用一些固定数量的线程来解决问题,每个线程在逻辑上独立且相对独立.您还可以在一个以上的线程中完成同一任务,将数据从一个线程传递到另一个线程.您所做的似乎没有任何实际意义.

如果您解释了应用程序的目标,则可能会对操作提出建议.仅发布代码转储并没有多大帮助.

—SA
The whole idea of spawning a new thread on some UI event is wrong even if you use the thread pool. You can easily deplete your system without any use. You need thread if you have logically different procedures to run in parallel. Think about it: what parallelism do you gain if you simply do the same things at once? Maybe, this is just experimenting, then I would understand. In real life you usually get get around just using some fixed amount of threads, one per each logically distinct and relatively independent activity. You can also work at the same task in more then one thread passing data from one thread to another. What you do does not seem to have any practical sense.

If you explain the goals of your application you might have an advice on what to do. Just posting a code dump does not help much.

—SA


这篇关于线程池数量过多,最多200个线程!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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