File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic


Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
What will happen if you compile/run this code?
1: public class Q1 extends Thread
2: {
3: public void run()
4: {
5: System.out.println("Before start method");
6: this.stop();
7: System.out.println("After stop method");
8: }
10: public static void main(String[] args)
11: {
12: Q1 a = new Q1();
13: a.start();
14: }
15: }
A) Compilation error at line 7.
B) Runtime exception at line 7.
C) Prints "Before start method" and "After stop method".
D) Prints "Before start method" only.

Okay, stop() has been deprecated since 1.1, pls use
interrupt() ....
But why is c) the output
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
In my machine it only prints... Before start method and the JVM exits. This is the correct behavior. stop() stops .. kills the current thread and exits.
Amit Goel
Ranch Hand

Joined: Dec 07, 2000
Posts: 50
hi i worked out on this code but it printed "before start method"
only as after it comes to this.stop() the thread dies.

Amit<br /> <br />The Less I have, The more I gain..Off the Beaten Path, I Reign.
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
I have the same result as others. It prints "before ..." and that's it.
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi Praveen,
Actually, I differ from others in that I got C) as output in my program. I browsed through Java Tutorial, and it says, the way to kill a thread or stop a thread is only by exiting the run method and nothing else.
Since stop() is deprecated I used interrupt().
But still, when I executed your code, I got
Before start method
After stop method
as output.
Execute the following code:
public class Q1 extends Thread
public void run()
System.out.println("Before start method");
System.out.println("Thread is interrupted by interrupt() method!");
System.out.println("After stop method");
public static void main(String[] args)
Q1 a = new Q1();
This shows that the thread has got interrupted by the interrupt() method. But you need to specify what needs to be done if the thread is interrupted, otherwise, the thread resumes its original set of statements in the run() method and stops when the run() method is exited.
Hope I am clear.

Joined: Dec 10, 2000
Posts: 3

Working with the above code using stop(); or interrupt(); the same output and that is D) Prints "Before start method" only.As after either of these methods are executed the current thread gets killed and exits.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Mukti,
Welcome to JavaRanch
Please read the naming policy and re-register using a name that complies with the rules.
Your cooperation would be appreciated.
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Mukti Bajaj
Ranch Hand

Joined: Dec 11, 2000
Posts: 59
Hi Jane,
I have re-registered
I agree. Here's the link:
subject: Threads
It's not a secret anymore!