Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it bad to pause/abort threads?

My model of how threads work is that some ThreadManager gives each thread a turn. When it's a thread's turn, it gets to execute a few lines of code.

To pause a thread, couldn't one just have the ThreadManager (momentarily) stop allowing that thread to have a turn?

To abort a thread, couldn't the ThreadManager just never give that thread another turn?

What's the problem?

like image 988
user420667 Avatar asked Jun 02 '11 03:06

user420667


People also ask

Will pause the thread and allow other threads to restart?

The suspend() method of thread class puts the thread from running to waiting state. This method is used if you want to stop the thread execution and start it again when a certain event occurs. This method allows a thread to temporarily cease execution. The suspended thread can be resumed using the resume() method.

What does thread abort do?

If the thread that calls Abort holds a lock that the aborted thread requires, a deadlock can occur. If Abort is called on a thread that has not been started, the thread will abort when Start is called. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.

Can you pause a thread?

Thread. sleep() method can be used to pause the execution of current thread for specified time in milliseconds.

What is the difference between thread interrupt and thread abort?

Abort method throws a ThreadAbortException, the Thread. Interrupt method throws a ThreadInterruptException. Essentially, a call to the Thread. Interrupt method interrupts the thread and throws a ThreadInterruptedException to interrupt the thread inside of a blocking call.


1 Answers

Quote from MSDN about pausing threads:

You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked. Deadlocks can occur very easily.

Aborted thread can lead to unpredicted circumstances. There is a good article about this: http://www.bluebytesoftware.com/blog/2009/03/13/ManagedCodeAndAsynchronousExceptionHardening.aspx

like image 94
Alex Aza Avatar answered Sep 22 '22 10:09

Alex Aza