• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Sleeping and interrupting  RSS feed

 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Java fellas,
Can someone please explain me, how the sleep() works and what happens to 'Thread.interrupted()' value when we interrupt a thread.
Thanks in Advance.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jude ,

how the sleep() works and what happens to 'Thread.interrupted()' value when we interrupt a thread.


Both sleep() and interrupted() are static methods .Both creates effect on the currently executing thread..

The Thread.sleep() method effectively "pauses" the current thread for a given period of time.The Thread.sleep() method essentially interacts with the thread scheduler to put the current thread into a wait state for the required interval. In order to allow interruption, the implementation may not actually use the explicit sleep function that most OS's provide.

When we interrupt the current thread the Thread.interrupted() will return true , otherwise it will return false.




few more thing Im adding here :

Use case -1 :
The static interrupted() method returns the interrupted flag after that it sets the flag to false if it is true.



Use case -2 :interrupting a thread that stops working
after interrupting the thread, we are propagating it, so it will stop working.



This code will stop working and throw InterruptedException..

Use case 3 : This is the continuation of the Use case -2 ..
after interrupting the thread, we handle the exception, so it will break out the sleeping but will not stop working.




If any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException. If the thread is not in the sleeping or waiting state, calling the interrupt() method performs normal behaviour and doesn't interrupt the thread but sets the interrupt flag to true.

Hope you understand ..

Thanks ,
Satya
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry bro, I didn't get it.
Below is my code i tried.

Output :

[main] Sleeping in main thread for 5s...
[main] Interrupting myThread
[main] Sleeping in main thread for 5s...
insdide catch() Interupted val:::false
[myThread] Interrupted by exception!
Interupted val:::false
[main] Interrupting myThread
[myThread] Interrupted for the second time.

I don't understand how that line number 7 saying that 'Thread.interrupted()' =false. I thought at that point, that static method should return true. Because i have interrupted it in my main(). That is why we came into catch() block. aren't we?
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't understand how that line number 7 saying that 'Thread.interrupted()' =false. I thought at that point, that static method should return true. Because i have interrupted it in my main(). That is why we came into catch() block. aren't we?


Jube , first of all you are creating two thread here one is the main thread and the other one is the myThread.Check the snapshot below :

Next : The behavior is as expected .
Reason :
When you start the myThread , it is going to the sleep and we know when one thread is going to sleep it holds the lock .Here is the trick bro ...
In the line line 30 the app is interrupting the myThread :



That is the reason the myThread throws the exception and exection gone to the catch block .
and you get the output !!!

According to the JSL :

interrupted
public static boolean interrupted()
Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it).
A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.


Returns:
true if the current thread has been interrupted; false otherwise.
See Also:
isInterrupted()

And

interrupt
public void interrupt()
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.


If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.


Throws:
SecurityException - if the current thread cannot modify this thread

That is the reason the while is looping infinite time and when you call second time myThread.interrupt(); the execution is being stopped ..


Thanks ,
Satya
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mate
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!