穿线速度 [英] Thread speed

查看:67
本文介绍了穿线速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的VB.NET应用程序接收数据时,它会按名称"创建一个线程,然后将数据添加到该线程的ConcurrentQueue中. 下次姓名"数据进入后,会将数据添加到现有线程的ConcurrentQueue中.
在线程内部,它从ConcurrentQueue中取出数据,然后线程将数据写入数据库.
有很多名称",因此有很多线程,大约200-300.

在下面的代码中,在Sub ProcessIt内部,如果我不输入#1代码(Thread.sleep(1)),则所有线程都以相同的速度写入数据库(我从时间戳中知道这一点)这是写在数据库中的),并且当1个线程写入该线程的速度很慢时 数据库(由于要写入的数据量),其他线程也很慢.

如果我放置#1代码(Thread.sleep(1)),则将数据较少的线程比数据较多的线程写入数据库的速度更快.
例如,"Name1"的线程已经从11:19:00 am写入数据,而"Name2"线程由于存在更多的"Name2"信息,因此从上午11:18:50写入数据.要写入的数据.

但是,我不认为每次启动sleep(1)都是一个好主意,对吗?因为我认为每次启动都会使写入数据库的速度降低1毫秒.
那么,我应该改为放置#2代码,即每100个实例我只睡觉一次(1)吗?
如何使每个线程的速度独立于其他线程?

该计算机是具有4个处理器(2.13 GHz)和8 gig RAM的64位OS Win Server 2008 R2 Standard虚拟机.

谢谢.

朋友HtWriteDB作为集合
HtWriteDB =新集合

如果HtWriteDB.Contains(Name)然后
Dim oWrite as clsWriteDB = DirectCast(HtWriteDB(Name),clsWriteDB)
    oWrite.AddItem(fields)
别的
Dim oWrite作为新的clsWriteDB
    oWrite.AddItem(fields)
    HtWriteDB.Add(oWrite,名称)
    ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf oWrite.ProcessIt))
万一

公共类clsWriteDB
    朋友qWrite为新的ConcurrentQueue(Of String())
    私有终止为布尔值=假

    Public Sub AddItem(ByVal oCalc作为对象)
        尝试
            qWrite.Enqueue(oCalc)
        异常捕获
        结束尝试
    结束子
    Public Sub ProcessIt()
        昏暗的fields()作为字符串
        Dim x As Int16 = 0

        虽然(未终止)
如果qWrite.Count> 0然后
qWrite.TryDequeue(字段)
:
万一
'#1
                线程睡眠(1)
                
'#2
x + = 1
                如果x> = 100那么
                线程睡眠(1)
                        x = 0
                万一

解决方案

首先考虑使用线程.

更多线程总是比一个线程花费更多的处理时间,尽管当存在真正的异步进程时,线程可以加快吞吐时间.

如果我查看您的代码,就我所知,您不要让子线程进入睡眠状态,而是让主线程进入睡眠状态.

只需避免将线程休眠在子线程中.子线程不是一种正在运行的服务,而是必须完成的任务.


As my VB.NET application receives data, it creates a thread per "Name", and add the data to the thread's ConcurrentQueue.
Next time data for "Name" comes in, it will add that data to the ConcurrentQueue of the existing thread.
Inside the thread, it dequeue the data from the ConcurrentQueue, then the thread writes the data to a database.
There are many "Name", thus there are many threads, about 200-300.

In the codes below, inside Sub ProcessIt, if I don't put the #1 Code (Thread.sleep(1)), all threads are written to the database at the same speed (I know this from the time stamp that's written in the database), and when 1 thread is slow in writing to the database (because of the amount of data to be written), the other threads are slow, too.

If I put #1 code (Thread.sleep(1)), thread with fewer data is written to the database faster than thread with more data.
For example, thread for "Name1" already writes data from 11:19:00 am, while thread for "Name2" writes data from 11:18:50 am since there are more "Name2" data to be written.

But, I don't think sleep(1) for every itiration is a good idea, is it ? Because I would think it slows down writting to the database by 1 ms per itiration.
So, shall I put #2 code instead, where I only sleep(1) every 100 itiration ?
How do I make the speed of each thread independent of the other threads ?

The machine is a virtual machine 64 bit OS Win Server 2008 R2 Standard with 4 processors (2.13 GHz) and 8 gig of RAM.

Thank you.

Friend HtWriteDB As Collection
HtWriteDB = New Collection

If HtWriteDB.Contains(Name) Then
Dim oWrite As clsWriteDB = DirectCast(HtWriteDB(Name), clsWriteDB)
    oWrite.AddItem(fields)
Else
Dim oWrite As New clsWriteDB
    oWrite.AddItem(fields)
    HtWriteDB.Add(oWrite, Name)
    ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf oWrite.ProcessIt))
End If

Public Class clsWriteDB
    Friend qWrite As New ConcurrentQueue(Of String())
    Private Terminated As Boolean = False

    Public Sub AddItem(ByVal oCalc As Object)
        Try
            qWrite.Enqueue(oCalc)
        Catch ex As Exception
        End Try
    End Sub
    Public Sub ProcessIt()
        Dim fields() As String
        Dim x As Int16 = 0
	
        While (Not Terminated)
		If qWrite.Count > 0 Then
			qWrite.TryDequeue(fields)
			:
		end if
		'#1 
                Thread.Sleep(1)
                
		'#2
		x += 1
                If x >= 100 Then
                	Thread.Sleep(1)
                        x = 0
                End If

解决方案

First of all think for you begin if you use threads. 

More Threads cost always more processing time than one thread, although when there are real assync processes, threads can speed up the throughput time. 

If I look at your code you are in my perception not let the sub thread sleep but the main thread.

Simply avoid sleeping the thread in a subthread. A sub thread is not a kind of running service but a task which has to be done.


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

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