Friday, September 20

java.lang.Thread.interrupted() method oncoked twice ..

                            java.lang.Thread.interrupted() method oncoked twice ..




If java.lang.Thread.interrupted() method is invoked twice , In second execution it will always return true.

Why?

Thread.interrupted() method does two things

1. returns the current status
2. clears the interrupted flag . i.e. set the interrupted flag of thread to false.


So at the time of first execution what ever might be the status of interruption flag of thread , it will follow above tow steps

1. return current status (true or false i.e. interrupted or not-interrupted)
2. clears the interrupted flag . i.e. set the interrupted flag of thread to fa
lse.

And at the time of second execution , as status after first execution is already set to false So

1. return current status (false)
2. clears the interrupted flag . i.e. set the interrupted flag of thread to false.

Thus is case of two successive execution of Thread.interrupted() method second execution will always return false.


So What is the purpose of Thread.interrupted() method at all in java




Sole purpose is to know the current interruption status of thread and also clearing the interruption status.







No comments:

Post a Comment