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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Question" Watch "Threads Question" New topic
Author

Threads Question

Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
hi all,
the following program compile but hangon at runtime
public class G124 extends Thread
{
G124 a=new G124();
public void run()
{
a.a();
}
public synchronized void a()
{
int i=0;
while(i++<3)
System.out.println("A "+i);
}
public static void main(String [] args)
{
G124 a=new G124();a.start();
G124 b=new G124();b.start();
}
}
------------------
Muhammad Hussain

Muhammad Hussain<br />Sun Certified Java Programer (SCJP2)<br />Sun Certified Web Component Devloper SCWCD
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10

When you create a new instance of G124 in Line 2, it causes the creation of a new instance of the instance variable in Line 1, which causes the creation of a new instance of the instance variable in Line 1, which causes the creation of a new instance of the instance variable in Line 1, which causes the creation of a new instance of the instance variable in Line 1, ...


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
thankx Marilyn deQueiroz

i got it..........
actually i m making this more simple
that is
at Line 1 the Object creates another G129 Object and another..................................................................................another........................................
------------------
Muhammad Hussain
farrukh mahmud
Ranch Hand

Joined: Feb 13, 2001
Posts: 47
Hi Marilyn deQueiroz
can u plz explain it because i am unable to grasp it or any other java Guru
Thanks

------------------
Regards
Farrukh Mahmud


Regards<BR>Farrukh Mahmud
Andre Zhang
Greenhorn

Joined: Aug 14, 2001
Posts: 9
Let's make the code simple first.
public class G124 extends Thread
{
G124 a = new G124();
public static void main(String[] args)
{
G124 a = new G124();
}
}
it will causes a hang-on at runtime because the unstoppable creation the a.
change this code a little more at the class declaration.
public class G124
{
G124 a = new G124();
public static void main(String[] args)
{
G124 a = new G124();
}
}
it causes a runtime error -- java.lang.StackOverflowError
my Question is: what's the difference between the two & why?
Mathew Kuruvilla
Ranch Hand

Joined: Nov 27, 2001
Posts: 135
Hmmm! Nobody seems to have an answer for Andre's question. . . . My guess is that when creating instances of Thread subclasses, the JVM doesn't use the stack to create the instance, but uses the heap instead. On the other hand, for non-thread objects, the JVM uses the stack to hold some values. Therefore when the recursive creation of instances occurs, for Thread extended objects, it keeps going till some resource is exhausted (RAM, I think). For non-Thread objects, the stack overflow condition occurs earlier because the size of the stack is (???) is smaller than the amount of RAM available.
Bartender, we need a strong cup of Java here. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Question