19
Answers

Threading

David Smith

David Smith

13y
2.6k
1
 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?



Answers (19)
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
I think you've grasped it :)

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.
Accepted
0
David Smith

David Smith

NA 1.9k 0 13y
0
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 13y
Yes, it will help very much if you put a link in the new thread to this one and also put a link in here to the new thread.

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.
0
David Smith

David Smith

NA 1.9k 0 13y
Lol @ Amit. How do you Link the thread to another. I assuming copying the new URL to the new posts. How do you normally do it.
0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 13y
ha ha ha.. I see the "Mr." thing is trending here lolzz.. anyways Darnell we can start a new thread on synchronization but don't forget to link the new thread at bottom of this Thread so that people can follow it.

0
David Smith

David Smith

NA 1.9k 0 13y


@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.....
0
David Smith

David Smith

NA 1.9k 0 13y
Hi Mr. Hobbs I havent spoke to you in years lolol. Tell me you didnt think or assume we were not reading, Not Mr. Hobbs couldn't be. I would hope you would of thought we were reading. If not im disappointed lolololol Jk. Keep in my mind everybody across the world learn in different ways. Not for sure if you understand that or not. Everybody is on different levels on coding. Back to the good members as you were saying. Thanks for the reply on thread that was a good thought . You mention reading, nevermind I will save that comment just in case you feel like you want to discuss anything other than threads. I have a Ebook on threading Csharp .net 4.0 its breaks down everything. I was helping other people out that were scared to ask questions. So I use myself to ask the questions and pointed them to this chat forum. Amit, Vuples, and Prabhu did a good job breaking down threads. We can read books all day,but  some people need to see examples. So I started a basic example. We are all not you Mr. Hobbs. You said these simple words that threading can be complex. Well lets try to break the complexity down as much as possible if that's not a problem.


@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.



0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
The only other item I'd like to add to your list, Amit, is thread pooling and I'll briefly say something about that.

As an alterative to creating your own threads, you can 'borrow' a thread from the thread-pool. When the thread finishes executing it is returned to the pool and can be re-used later which is not something you can do with threads which you create yourself. 

The advantages of the thread-pool are that:

1. It cuts down on the overhead of creating your own threads, notably the time needed to set up a stack which requires 1 MB of memory.

2. It controls the number of threads which are allowed to run simultaneously without overburdening the processor(s) or operating system. Once the limit is reached, requests for pool threads can be queued until threads already in use are returned to the pool.

In .NET 4.0, you can request a pool thread as follows:

  System.Threading.Tasks.Task.Factory.StartNew(SomeMethod);

In earlier versions, you can do the same with:

  System.Threading.Threadpool.QueueUserWorkItem(SomeMethod);

0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 13y
I like the way vulpes explained the topic so..

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.
0
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 13y
Mister Dudley, opinions of threads are not useful. What is useful is facts. Threads are a complex topic and you must do some reading to understand them. It is good that members here have helped you to understand, but you will need to also do some reading to understand.

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.
0
David Smith

David Smith

NA 1.9k 0 13y
I knew what a thread was all along to an certain extinct, but I never knew how to explain a thread in an understandable fashion when someone ask me.

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?



0
David Smith

David Smith

NA 1.9k 0 13y
Thanks for the reply vuples. So yes all methods are not a thread. So let me review review. A thread from any program starts from the Parent main thread. To specify or start a new thread, you will only have to call or create an instance of a thread object. If a thread object is not created, any method that is created or call is operating on one thread which will be the parent thread call Main(). How accurate is this statement?. Its starting to make sense. I have alot people say that a method is a thread, yea, but no.
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
Regarding the questions you raised in your penultimate post:

1. A thread begins with a method call and ends when the method returns. However, this doesn't mean that a particular method always executes on the same thread. It may in fact be executed by several threads concurrently.

This is possible because each thread has its own stack (to store parameters and local variables) and is granted  a 'time slice' by the thread scheduler which is responsible for switching execution between all running threads.
 
2. The main (or parent) thread begins when the Main() method is called and ends when the Main() method returns i.e. when the program terminates.

3. WhatIsAThreadAmit() will not run on a separate thread unless you create a new Thread object and pass it as the 'ThreadStart' parameter. In your example, the method will run on the main thread.

0
David Smith

David Smith

NA 1.9k 0 13y
@Prabhu ,

the website you sent is a great website explaining threads also for beginners.
0
David Smith

David Smith

NA 1.9k 0 13y
Thanks for your reply.

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);
    }));

}





0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 13y
Starting Point Example:
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.
static void Main(string[] args)
{
    Console.Write("Main Thread id: " + Thread.CurrentThread.ManagedThreadId);

    Thread th = new Thread(new ParameterizedThreadStart(() =>
    {
        Console.Write("I'm a new thread: " + Thread.CurrentThread.ManagedThreadId);
    }));
}
Output:


So here you can see threads in action.


0
David Smith

David Smith

NA 1.9k 0 13y
@Prabhu Raja I was asking how to create threads.  I want to start a discussion to get multiple opinions.  But thanks for  your website that you posted.


@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()
        {

        }

}




0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 13y
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.

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. 




0
Prabhu Raja

Prabhu Raja

NA 4.7k 1.5m 13y
Hi,

       If you are C# Programmer, It will help you. Click Here