• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

threads

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jasper...... So, the interrupt() method will interrupt the thread that has called it and not the other threads. Is that so?
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic