Advertisement



< Prev
Next >



C# ThreadInterruptedException



An exception of type ThreadInterruptedException is thrown, when the Interrupt() method is called on a thread which is in a blocked state i.e. WaitSleepJoin state, which is either of the wait state, sleep state or join state.

If the Interrupt() method is called on a thread which is currently not in a blocked state but enters either of the blocked states anytime late during its execution, the ThreadInterruptedException is thrown by it.

No ThreadInterruptedException exception is thrown by a thread which never goes to a blocked state during its lifetime.




Importance of try-catch block


A thread can be interrupted by calling the Interrupt() method on it and if the thread being interrupted is already in a blocked state or goes in a blocked state anytime during its execution, the ThreadInterruptedException will be thrown, so we must enclose the thread we want to interrupt with in the try-catch block, or we won't be able to catch the ThreadInterruptedException.




ThreadStateException, when a thread is interrupted from the sleep state.


In this program, we are going to interrupt a child thread(created from the Main Thread) by calling the Interrupt() method on it i.e. when this thread goes to sleep by calling the Sleep() method.

The thread after being interrupted throws the ThreadInterruptedException, which will be caught by its try-catch block.

//C# Example of Interrupt() method

using System;
using System.Threading;

class ThreadEx
{
Thread thread;

//Constructor of our class
public ThreadEx(String name)
{

	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;

	//Calling the Start() method, which calls the entry-point method defined with an argument
	thread.Start();
}

public void Run()
{
	//Getting the reference of the currently executing thread
	Thread th = Thread.CurrentThread;
	
	try
	{
		Console.WriteLine(th.Name + " has started its execution");
		for(int i=0; i<3; i++)
		{
			Console.WriteLine(th.Name + " is printing " + i);
		
			//Calling the Sleep() method on newly created thread
			//To make it sleep and suspend for 2 seconds after printing a number
			Thread.Sleep(2000);
		}
		Console.WriteLine(th.Name + " has finished its execution ");
	}
	catch(ThreadInterruptedException e)
	{
		Console.WriteLine("Thread Interrupted" + e);
	}
}


public static void Main(String[] ar)
{
	//Creating an object of our ThreadEx class
	//where each object is used to construct a separate thread
	ThreadEx ob1 = new ThreadEx("My Thread1");

	//Main thread calling Interrupt() on the new Thread
	//This will  throw ThreadInterruptedException when the new Thread is goes to a blocked state i.e. WaitSleepJoin
	ob1.thread.Interrupt();
	
	//Main thread has called Join() method on the new thread
	//To wait until its execution is complete
	ob1.thread.Join();
}
}


Output-


My Thread1 has started its execution
My Thread1 is printing 0
Thread InterruptedSystem.Threading.ThreadInterruptedException: Thread was interrupted from a waiting state.
   at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
   at System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
   at ThreadEx.Run()


Program Analysis





Advertisement




ThreadInterruptedException, when a thread is interrupted from the join state.


In this program, we are going to interrupt a child thread(created from the Main Thread) by calling the Interrupt() method on it i.e. when this child thread goes to a blocked state by calling the Join() method on the Main Thread, which makes it wait for the Main Thread to finish its execution.

The Main Thread after being interrupted throws the ThreadInterruptedException, which will be caught by its try-catch block.

//C# Example of calling the Interrupt() method on a child thread
//Which goes to a blocked state by calling Join() on the Main Thread
//Which makes this child thread wait for the Main Thread to finish its execution


using System;
using System.Threading;

class ThreadEx
{
Thread thread;

//Constructor of our class
public ThreadEx(String name, Thread main)
{
	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;

	//Calling the Start() method, which calls the entry-point method defined with an argument
	//And passing it an argument i.e. reference to the Main Thread
	thread.Start(main);
}


//Entry Point of the new thread.
public void Run(Object main)  
{

	try
	{
		//Getting the reference to the currently executing thread.
		Thread th = Thread.CurrentThread;

		Console.WriteLine(th.Name + " has started its execution");

		//Casting the value of main(which was internally an Thread), from Object to Thread
		Thread th2 = (Thread)main;

		Console.WriteLine(th2.Name + " is being interrupted");

		//This child thread is calling the Join() method on the Main thread
		//Which puts it in a blocked state i.e. waiting for the Main thread to finish its execution
		th2.Join();

		Console.WriteLine(th.Name + " has finished its execution ");
	}
	catch(Exception e)
	{
		Console.WriteLine("Exception caught: " + e);
	}
	
}


public static void Main(String[] ar)
{
	//Getting the reference of the automatically created currently executing Main thread
	Thread mainThread = Thread.CurrentThread;

	//Setting the name of the Main threadss		
	mainThread.Name = "Main Thread";
	
	//Creating an objects of our ThreadEx class
	//where each object is used to construct a separate thread
	//And passing the reference of the Main thread
	ThreadEx ob1 = new ThreadEx("My Thread1", mainThread);

	//Main thread calling Interrupt() on the new Thread
	//This will  throw ThreadInterruptedException when the new Thread is goes to a blocked state i.e. WaitSleepJoin
	ob1.thread.Interrupt();
}
}


Output-


My Thread1 has started its execution
Main Thread is being interrupted
Exception caught: System.Threading.ThreadInterruptedException: Thread was interrupted from a waiting state.
   at System.Threading.Thread.JoinInternal(Int32 millisecondsTimeout)
   at ThreadEx.Run(Object main)


Program Analysis






Please share this article -




< Prev
Next >
< C# Thread Join() Method
C# Abort() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement