Reputation: 581
I got a printing service that will now also do file integrations, in order to do both tasks side by side I though about using Threads.
After seeing this Thread Tutorial I then searched for what Join do.
If I'm not wrong, Join terminates the thread, right? So my question here, since the service should be running 24/7 until someone need to stop the Thread, can I just do a Thread.Sleep to make them wait for the next interaction instead of Join and Start them again?
Even if they are working in different methods with different objects can they still giving deadlocks? Since it's for a service I can't have it giving deadlocks times to times.
EDIT: I did the following test and it seems to be working well, anyome with more experience using threads can check if would cause me problems later? (This code is a test sample, it is not my service).
Upvotes: 0
Views: 47
Reputation: 156968
Join terminates the thread, right?
No, it waits until the thread is done.
can I just do a Thread.Sleep to make them wait for the next interaction instead of Join and Start them again?
Yes, you can do that. Depending on your architecture, you could also use a BlockingCollection
to queue items to process in a separate thread, and executing them in another.
Even if they are working in different methods with different objects can they still giving deadlocks?
Well, they can if you write your code the wrong way. There is no quick and easy fix for this, besides keeping in mind all the time that two methods could call some shared code at the exact same time, causing race conditions and deadlocks.
Upvotes: 1