10-02-2010 01:05 AM
I have an app where I will need a thread to run and stop at different times.
My question is would it be better to keep setting up my thread when I need it, or would it be better to haev the thread sleep and then intterupt it when I need it?
I've been trying to find the answer online but haven't found anything. I would imagine that a sleeping thread uses very little system reoures if at all. Howerver recreating the thread over and over again could be a strain on resources.
I'm just wondering because it isn't clear to me what the better option would be.
10-02-2010 01:19 AM
well i think it depends upon your requirement
if you need to work frequently inside the thread then i think u should create a single thread and put it to sleep when not required
10-02-2010 01:25 AM
I'm guess what I'm wondering is what takes up more system resources putting a thread to sleep and interrupting it or creating and destroying the thread.
Is there any real downside to keeing the thread alive and sleeping it rather then killing the thread and having to recreate the thread? I only have 2 threads in the whole app so it's not like I will have too many threads exception or anything like that. I'm just not sure if there are any side effects to a sleeping thread.
10-02-2010 01:39 AM
The biggest thing to think about is how often you will need to wake the thread and whether or not you know exactly when you need the tread working or if it is dynamic. If you have widly varying constraints then it would not be very reasonable to have to keep polling the thread because then it is effect constantly running.
10-02-2010 02:23 AM
I would suggest that... If you want to use that thread after short intervals(quite often) go for sleeping it... but if the interval is long and less frequent go for killing it and recreating it....
10-05-2010 08:01 AM
If the thread is only used to periodically invoke very short operations, consider using the event thread instead (Application.invokeLater with the specified time/periodicity to invoke the Runnable).