jQuery in Action, 2nd edition
The moose likes Mock Exam Errata and the fly likes Thread doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply 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: 343

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."
 
IntelliJ Java IDE
 
subject: Thread doubt
 
Threads others viewed
output
interrupting a thread
Thread Sleeping problem
Please explain this code
threads
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture