Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Praveen Zala
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: }
9:
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i worked out on this code but it printed "before start method"
only as after it comes to this.stop() the thread dies.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same result as others. It prints "before ..." and that's it.
 
Aparna Narayanan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
<CODE>
public class Q1 extends Thread
{
public void run()
{
System.out.println("Before start method");
interrupt();
if(this.isInterrupted())
{
System.out.println("Thread is interrupted by interrupt() method!");
}
else
System.out.println("After stop method");
}
public static void main(String[] args)
{
Q1 a = new Q1();
a.start();
}
}
</CODE>
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.
Aparna
 
Mukti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Working with the above code using stop(); or interrupt(); .....gives 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.
Thanks.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jane,
I have re-registered
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic