File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes Thread doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Thread doubt" Watch "Thread doubt" New topic
Author

Thread doubt

Sushanta Chakrabarty
Greenhorn

Joined: Dec 05, 2007
Posts: 4
Hi,

I am getting StackOverflowException when I run the following code.

Sushanta.

public class EE extends Thread{
EE e = new EE();
void q(){
synchronized(e){
try{
wait();
}
catch(Exception ex){}
}}
public static void main(String[]args){
EE e = new EE();
e.q();
}
}
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

IN your main, you create an EE, your EE has a EE member that in turn has an EE
member which in turn has an EE member which repeats to infinity ... or in your case your stack overflows.

Basically one of your new EE's shouldn't be there at a guess remove the member variable one and make the method sync on instead.

PS in 'real' code wait should always be in a loop testing an appropriate variable.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
ajay verma
Ranch Hand

Joined: Dec 10, 2007
Posts: 37
you should use super keyword in order to pass from super class
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I think your stack overflow derives somehow from havign two calls that look like this: EE e = new EE(); The rest is as given.

Most stack overflows descend somehow from this type of control-flow/logic-error mistake that looks like this.
[ March 30, 2008: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread doubt