aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How about this thread question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How about this thread question?" Watch "How about this thread question?" New topic
Author

How about this thread question?

Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Although the program is short but you have to
think very very carefully before you answer.
What happens when you compile and run:

Select one correct answer:
The program will
1. prints 'a' 1 time.
2. prints 'a' 2 times.
3. prints 'a' 3 times.
4. prints many 'a' more than 3 times and exits normally.
5. prints 'a' endlessly until out of memory.
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Oh! I forget something.
I also made up this question.
So,do you like it?
I like this question because this short program
will make your thinking process extremely long.
This time I wlll let somebody else explain the output result until you blackmail me for the
explanation.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
why on earth would anyone want to extend Thread while implementing Runnable??? :roll:
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
Pretty tricky but I'll give it a try. First of all it prints 'a' 2 times.
When you call st.start(), JVM will call st.run()
Calling st.run() causes the first 'a' to be printed.
Then in the next statement super.run() invokes the run() method of the super class, Thread, which calls the run() method of the Runnable object passed into the contructor, if there is one. In this case, the s object.
So the s.run() is called, which causes the second 'a' to be printed.
Then again in the next statement super.run() invokes the run() method of the super class, Thread, which calls the run() method of the Runnable object passed into the contructor, if there is one. In this case, there is none!
So s.run() exits, then st thread terminates.
Nice question!
[ June 20, 2002: Message edited by: Tybon Wu ]

SCJP2
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Alan
This program prints "a" twice.
My own explanation:
*********************************
s object is created
st object is created
- st started -> run() prints "a" and calls run() method of super class
-super class of Monster is Thread class. Thread.run() calls overrided
version (from Monster) and it prints "a" again.
-then again super.run() method (in Thread class, not in Monster class) is called.
It calls just Thread's empty run() method and ends.
Jamal Hasanov
www.j-think.com
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi Anthony,
Sorry, it was my mistake. Monster doesn't have to implement Runnable.
Hi Jamal,
Tybon's explanation is more detailed because Tybon point out "which object".
Hi Tybon,
I am glad that you like this question. You really
know Thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How about this thread question?
 
Similar Threads
Thread question
anonymous class question
Try this question I made up.
When both Thread and Runnable have public void run
Thread question