wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "threads" Watch "threads" New topic
Author

threads

sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
I am not sure how the method interrupt()works...

public class InterruptThread extends Object implements Runnable
{
public void run()
{
try
{ System.out.println("New Thread is about to sleep for 20 seconds.");
Thread.sleep(20000);

} catch(InterruptedException e)
{
System.out.println("in run()- interrupted while sleeping");
return;
}
System.out.println("in run() - doing stuff after nap");
}


public static void main(String args[])
{
InterruptThread it=new InterruptThread();
Thread t=new Thread(it, "child thread");
t.start();
try
{Thread.sleep(20000);
}
catch(InterruptedException e) {System.out.println("in run()- interrupted while sleeping");}
System.out.println("in main()-about to interrupt child thread");
t.interrupt();
System.out.println("in main() - leaving the main thread");
}
}

I have 2 questions here. 1. Is it necessary to extend class Object in this case or is it implicitly done.
2. In the main method of the class, does the t.interrupt act as a main thread and not as a child thread in this case. In this case, t,interrupt() interrupts the child thread.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

1. Is it necessary to extend class Object in this case or is it implicitly done.

It is done implicitly, you never have to extend Object explicitly.

2. In the main method of the class, does the t.interrupt act as a main thread and not as a child thread in this case. In this case, t,interrupt() interrupts the child thread.

You are calling interrupt() on the object that 't' points to, which is the child thread. So you are interrupting the child thread. You let the program sleep for 20 seconds in both threads. Note that the child thread will stop sleeping at approximately the same time as the main thread. So at the moment you call t.interrupt(), you can't be sure if the child thread is still sleeping, or if it already finished sleeping.

Try changing the Thread.sleep(20000); in the main thread to Thread.sleep(10000); - that way you can be sure that the child thread is still sleeping when you call t.interrupt().
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
Thanks Jasper..... So, interrupt() method will interrupt the thread which has called it, i.e, if a child thread calls the interrupt() then the child thread gets interrupted. Is it so?
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
Thanks Jasper...... So, the interrupt() method will interrupt the thread that has called it and not the other threads. Is that so?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Originally posted by sai donthneni:
Thanks Jasper...... So, the interrupt() method will interrupt the thread that has called it and not the other threads. Is that so?


The interrupt() method will send the interrupt to the thread that is represented by the Thread object.

It doesn't make sense to interrupt the current thread. You interrupt a thread to get it to stop what it is doing, in order to get it to do something else.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
Thanks Henry, if I have to interrupt the main thread then should I use it this way.

Thread.currentThread.interrupt() in the main method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Originally posted by sai donthneni:
Thanks Henry, if I have to interrupt the main thread then should I use it this way.

Thread.currentThread.interrupt() in the main method.


Sorry for being sarcastic... but this is kindof funny...

Why would you want to interrupt the current thread? Basically, you want to stop the current thread (from interrupting the current thread), and go do something else? Wouldn't it just be easier to just go do that something else?

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: threads
 
Similar Threads
Threads
Quest : Interrupt, isInterrupted, interrupted
Threads
closing thread and its childs
Interrupt in Threads