Help on understanding thread

What are threads and what are the Pros and Cons
rayluvsAsked:
Who is Participating?
 
DhaestConnect With a Mentor Commented:
Understanding Threading in .NET Framework
http://www.codeproject.com/KB/threads/threadinginnet.aspx
0
 
rayluvsAuthor Commented:
To be more specific, we came upon the term "thread" in visual basic .Net when an application we are working in became super slow when processing.   The reason is that the apps has to run a series a routine for different companies.  The way is set up now, the routines are run one by one, company by company; hence super slow.

While googling we found that instead calling the subroutine directly, we call it via "thread".  The process or routine is executed and the controls return and can be used to run the next company.  This in turns make the subroutine run simultaneously.

We are not knowledgeable yet in using thread and since it's a lot code, we wanted an experience opinion on EE behalf.

What you guys think of of thread?
When should we really consider using this method?
Any ram or memory considerations in the PC?

Thnax
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
CodeCruiserConnect With a Mentor Commented:
It really depends on your code and we would not be able to give proper advice without seeing the code and understanding what it does. Generally speaking, you can run any method on a separate thread as long as there is no UI code in that method.

It appears that in your case, a queue of tasks and a pool of threads would do the trick. But you would need to make sure the threads do not try to access same resource simultaneously and if they do then you would use locking.
0
 
rayluvsAuthor Commented:
Thanx for the links, but can you gives us in your own experience or words?  We mean like what have been your experience when you first came upon thread.  Also how did you work around when incorporating thread to your existing code.

Greatly appreciated
0
 
rayluvsAuthor Commented:
Understood.  We don't expect direct advice as to our own developing environment, just some experience guidance on the matter.

Thanx!
0
 
HooKooDooKuConnect With a Mentor Commented:
Think of a thread like a program.  Just like you can run multiple applications simultaneously on a single computer, you can run multiple threads simultaneously.  

Of course actually, the threads/programs do not run "simultaneously", they instead execute in time slices from the CPU.  Basically, the CPU executes one program/thread for a few moments, then just to the next program/thread for a few moments, and back again.  (With some modern multi-core CPUs, some of the logic will actually execute simultaneously as one core might be working on one thread/program while the other core works on another thread/program.)

However, where programs run in their own memory space (so that one program can not access data from another program), multiple threads within one program do run in the same memory space.  As such, the logic the thread is running must be made "thread safe".  As an example, if two threads could access the string variable A$, thread A might attempt to execute the command "A$="ABCDEFG", and thread B might attempt to execute the command "A$="123456".  Thread A might get as far as writing "ABC" before thread B comes in and starts writing "1234", then thread A might write "DE", then thread B writes "56", etc.  By the time the two threads finish writing to A$, you wind up with something like "123D5FG".  (In reality the results would be worst, because creating a string involves many steps including allocation of memory, so at the two threads attempt to write to the same string, nasty things might really happen).

So depending upon your processor, going multithreaded might or might not improve the overall performance (because in a single CPU core machine, the overall process will take longer as all the same work has to be done, but the CPU has to add the overhead of swapping between threads).  But the single biggest issue you will face is what CodeCruiser points out... the threads accessing resources simultaneously.
0
 
apumaConnect With a Mentor Commented:
I have found that if your code spends allot of time waiting for multiple responses from outside sources (web services, Databases etc..) then adding threading can significantly increase performance

I.E. if your application is doing something like this  (and thee operations don't rely on the previous operation completing)
getDBResult (2 Seconds)
getanotherDBResult (3 seconds)
sendanemail (0.5 Seconds)
writeaDBRecord (0.5 Seconds)

it would take 6 seconds to complete.  (2+3+0.5+0.5)

whereas this
Threading.ThreadPool.QueueUserWorkItem(AddressOf getDBResult)
Threading.ThreadPool.QueueUserWorkItem(AddressOf getanotherDBResult )
Threading.ThreadPool.QueueUserWorkItem(AddressOf sendanemai)
Threading.ThreadPool.QueueUserWorkItem(AddressOf writeaDBRecord)
Wait for all threads to complete  

in theory this would only take 3 seconds (the longest individual operation)

but there is allot more management to perform for thread safety and control as pointed out in the other posts.
0
 
rayluvsAuthor Commented:
Thank you very much!
0
 
rayluvsAuthor Commented:
Thanx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.