• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Threads Question

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. :-)
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic