File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Question

 
Hussain
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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, ...
 
Hussain
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marilyn deQueiroz
can u plz explain it because i am unable to grasp it or any other java Guru
Thanks

------------------
Regards
Farrukh Mahmud
 
Andre Zhang
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic