Threading Performance gain

rali08

Member
Joined
Nov 1, 2006
Messages
8
Programming Experience
1-3
i implemented a thread using the code below. The get device sql query takes 12 seconds to execute and then binds to a dropdownlist. i figured if i spawn it on a different thread, i would gain time becuase it is executing in paralell. I havent seen those gains. Can you help


VB.NET:
Dim threadSimpleExtract As Thread
threadSimpleExtract = New Thread(AddressOf Get_DeviceType)
threadSimpleExtract.Name = "Data"
threadSimpleExtract.ApartmentState = ApartmentState.MTA
threadSimpleExtract.CurrentPrincipal = HttpContext.Current.User
threadSimpleExtract.IsBackground = True
threadSimpleExtract.Priority = ThreadPriority.Highest
threadSimpleExtract.Start()
right before the page load i do
threadSimpleExtract.Join()

VB.NET:
Get device code
Dim dbBAM As New BeAManager.Database
Dim prBAM(2) As OracleClient.OracleParameter
Dim prBAM1(1) As OracleClient.OracleParameter
Dim drBAM As OracleClient.OracleDataReader
Dim wi As System.Security.Principal.WindowsImpersonationContext = Global.ApplicationIdentity.Impersonate()
' SyncLock cboDeviceType.GetType
prBAM1(0) = dbBAM.MakeParameter("P_Study", studyID)
prBAM1(1) = dbBAM.MakeOutputParameter("OCursor")
dbBAM.RunProcedure("CRIS_S_DEVICETYPE", prBAM1, drBAM)
cboDeviceType.DataTextField = "DEVICENAME"
cboDeviceType.DataValueField = "ITEMID"
cboDeviceType.DataSource = drBAM
cboDeviceType.DataBind()
cboDeviceType.Items.Insert(0, "-Select Device Type-")
drBAM.Close()
drBAM.Dispose()
dbBAM.Close()
dbBAM.Dispose()
' End SyncLock
wi.Undo()
 
Doing something in one thread or another doesn't make any difference for processing time. If a single processing job takes 12 seconds in one thread, it takes 12 seconds in another also, there is no difference.

Multi-threading is a way of organizing processing different things in parallell. A key element is idle processing time, another is GUI interference.

Usually multithreading is done to relieve the GUI thread having to wait for worker to finish, it lets the user do user stuff, then suddenly the worker returns results.

Another example is typically calls to different servers, calls that make the request and waits idle for results. If done in succession the time would accumulate with the all the idle wait for response, when done in multithreaded parallel all calls are made almost at the same time and they all waits simultanously then gets results one after the other. Visualized:
Single thread:
T1 call server1, wait 1 second, get response, T1 call server2, wait 1 second, get response.
Total time 2.01 seconds.
Multi-thread:
T1 call server1, wait 1 second, get response,
_T2 call server2, wait 1 second, get response.
Total time 1.01 seconds.
 
... it takes 12 seconds in another also, there is no difference.
I have to correct myself a little here. If going from a linear single thread that give the thread all CPU power, to running more than one processing at the same time, there may actually be a performance loss when measuring each individual process. This because there is a limit to how much CPU power there is and when running two or more threads they may have to share the CPU cycles. But the point about idle time remains, also about freeing GUI.
 
Ok i get the threading setup. However based on the example you gave the second one is done in 1 second, compared to 2 second linerly. If your running this on a quad processor system, there should be a performance gain. If the total process had two steps at the first took 12 seconds and the second took 5. If you ran it parallel you should get a 5 second gain becuase the main thread does not have to wait for one process to finish
 
Perhaps, but then again sometimes part of a process depend upon the other. The example was intended for connection to different servers, a server is usually not the local machine.
 
Back
Top