In helping someone with threading assignment this week, Threading was being use all for e-commerce system. Its not something I would normally do. So I want to
start the with the simple questions. What is a thread?
What is a thread?
When to use a thread?
When not to use thread?
Loading
VulpesPosted Oct 4, 2011, 10:40 AM
Note though that if you're running a method on a separate thread and that method calls another method, then the second method will also run on the separate thread because the first method hasn't returned yet.
David SmithPosted Oct 4, 2011, 8:53 PM
http://http://www.c-sharpcorner.com/Forums/Thread/142472/thread-synchronization.aspx
Sam HobbsPosted Oct 4, 2011, 7:06 PM
The reason I say that threads are complex is that the subject is the type of thing that can be made to sound clear but when it gets to actually doing something it might be very unclear.
In these forums and most other forums, questions get more specific and more helpful answers when they ask specific questions. It is very common for people to ask very general questions and they often don't get much of an answer. People spend a lot of time writing articles to answer very general questions such as yours.
David SmithPosted Oct 4, 2011, 4:10 PM
Amit ChoudharyPosted Oct 4, 2011, 3:18 PM
David SmithPosted Oct 4, 2011, 2:38 PM
@Amit, @Vuples, and Mr. Hobbs
What is your definition of Synchronization?
Can you point out some weird issues when dealing with Thread Synchronization? In my past experience. I experience a lot timing issues etc.....
David SmithPosted Oct 4, 2011, 2:35 PM
@Amit and @Vuples,
We can start Synchronization. Do you guys want set up another thread dealing the next topics or continue to use this thread.
What is Synchronization?
Well my definition is to ensure that your sets are completely processed before you gets happen. These issues happens when you have threaded methods that are not Asynchronus methods. This is were locks and semaphores comes into place.
VulpesPosted Oct 4, 2011, 2:23 PM
Amit ChoudharyPosted Oct 4, 2011, 1:19 PM
Next step can be,
Synchronization, Communication and Events among Threads. :)
Vulpes you may want to add more items here to the next step of Threads.
Sam HobbsPosted Oct 4, 2011, 1:16 PM
Other operating systems use other terms (such as tasks) for the things that Windows calls threads. In the simplest form, a thread is something that is used to keep the processor busy. Programs need to wait for things, such as a disk drive to read and write. While the IO is being done the processor must wait, so threads can be used to do other things while one of them is waiting.
That is a simplifed example and it makes it very clear what a thread is for. There are many real-world situations in which it is not clear when to use a thread. Such as the discussion last week about interfacing with a robot. For that, it is difficult to decide when to use threads.
.Net often uses threads at times that you might not realize. When it does, you probably do not need to use a thread. It is very easy for a beginner to not use a thread when it would be useful to do so and it is easy to think a thread is needed when it is not.
David SmithPosted Oct 4, 2011, 12:57 PM
If I would give a simple definition for a thread. It would be Multiple processes working parallel with each other sharing the same Heap stack.
I started this conversation to help others out. I alot of people are afraid to ask questions. So I basically setup a basic chat so others and I can truly grasp the concept of using
threads.
I normally using multiple threads when I am importing and processing a large amount of data. I have the main thread importing the data and another thread processing the data.
Thanks.
What would be a follow up topic regarding thread to learn and discuss now that we know what is a thread?
David SmithPosted Oct 4, 2011, 10:35 AM
VulpesPosted Oct 4, 2011, 9:44 AM
David SmithPosted Oct 4, 2011, 9:22 AM
the website you sent is a great website explaining threads also for beginners.
David SmithPosted Oct 4, 2011, 9:16 AM
1.So would you a agree, If I said a thread is somewhat a function?
2.In this program is the parent thread the "main():?
3. Just so that I am clear. Lets go from your example. I added a method call WhatIsAThreadAmit(). If I were to call WhatIsAThreadAmit() within Main thread
Would WhatIsAThreadAmit() method be consider a thread?
static void Main(string[] args)
{
Console.Write("Main Thread id: " + Thread.CurrentThread.ManagedThreadId);
WhatIsAThreadAmit();
Thread th = new Thread(new ParameterizedThreadStart(() =>
{
Console.Write("I'm a new thread: " + Thread.CurrentThread.ManagedThreadId);
}));
}
Amit ChoudharyPosted Oct 4, 2011, 5:40 AM
namespace Program
{
void main()
{
}
}
okay, Your program will be executing under a process started by your .exe file. So the Main() will be executing as a thread in the process and Will be the main thread in the execution. Now let say you want to create a separate thread then you will be invoking it from inside the Mail()
for e.g.
{
Console.Write("Main Thread id: " + Thread.CurrentThread.ManagedThreadId);
Thread th = new Thread(new ParameterizedThreadStart(() =>
{
Console.Write("I'm a new thread: " + Thread.CurrentThread.ManagedThreadId);
}));
}
So here you can see threads in action.
David SmithPosted Oct 4, 2011, 2:26 AM
@Amit Thanks for your reply. That was a good reply.
"Thread - Is itself a process that belongs to its parent process and it runs and behave just like other processes but always bound to the boundries of its parent. In a multiprocessing environment there can be multiple threads running under a single process. In programming a Thread is a class that get invoked under the Main thread with its own context and domain."
As far a code you say a method is a thread? The method "void main()" below. The entry point main method below would you call that a thread? Lets take a basic example. I really want to break down a thread in detail. Ok we have a definition. Now lets apply it in code.
Starting Point Example:
namespace Program
{
void main()
{
}
}
Amit ChoudharyPosted Oct 4, 2011, 1:58 AM
When to use Thread - Well, This is the decision that you always have to take care when to use threading. The simplest scenario is sending bulk mails in case of business websites. It could be done using threads that will run a separate process and your main thread won't wait for task to complete. Also you can use Async behavior to keep everything responsive cause if Main thread completes first then It'll wait for its child thread to complete. So whenever you want to perform a task in background or in parallel go for threading mechanism.
When Not to use Thread - When you don't want to do anything in parallel or as background process. when your application is efficient to complete the task in a single thread means user can wait for the task to complete no long waits.
Using threads definitely makes user experience good as everybody hates to wait for a task to respond in lazy manner. But always take decision wisely.
Prabhu RajaPosted Oct 4, 2011, 1:43 AM
If you are C# Programmer, It will help you. Click Here