File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes DOes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "DOes "while(true)" condition make a loop endless ?" Watch "DOes "while(true)" condition make a loop endless ?" New topic
Author

DOes "while(true)" condition make a loop endless ?

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
Does while(true) loop make above loop endless ? In the answers it says "Runtime error, an exception will be thrown

A call to wait/notify must be within synchronized code. With JDK1.2 this code throws the error message"

It does not say anything about making loop endless. I guessed, it will throw an exception "StackOverflow exception" because while(true) condition is always true. If wait() and notifyAll() are not there in the code, will it throw StackOverflow exception ?

I JUST TESTED CODE BY TAKING OUT NOYIFYALL() AND WAIT(). I WAS RIGHT, IT DOES END UP IN ENDLESS LOOP. I DID NOT WAIT FOR IT TO THROW STACKOVERFLOW EXCEPTION AND JUST TERMINATED PROCESS. WILL IT THROW THIS EXCEPTION AT LAST OR NOT ?

Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

nirjari patel wrote:Does while(true) loop make above loop endless ? In the answers it says "Runtime error, an exception will be thrown

A call to wait/notify must be within synchronized code. With JDK1.2 this code throws the error message"

It does not say anything about making loop endless. I guessed, it will throw an exception "StackOverflow exception" because while(true) condition is always true. If wait() and notifyAll() are not there in the code, will it throw StackOverflow exception ?

I JUST TESTED CODE BY TAKING OUT NOYIFYALL() AND WAIT(). I WAS RIGHT, IT DOES END UP IN ENDLESS LOOP. I DID NOT WAIT FOR IT TO THROW STACKOVERFLOW EXCEPTION AND JUST TERMINATED PROCESS. WILL IT THROW THIS EXCEPTION AT LAST OR NOT ?


Thanks



Why would an endless loop cause a stack overflow?

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
The question, and the answer, are about what happens when the wait() and notifyAll() are in the code. If you test without those methods, you will get very different behavior, which has little to do with the original question. So it's not clear whether you're interested in the original question, or your own new question. Why are you changing the code?
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

nirjari patel wrote:
WILL IT THROW THIS EXCEPTION AT LAST OR NOT ?


I guess it won't. Garbage Collector clears allocated memory space for JVM.


Are you better than me? Then please show me my mistakes..
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Supun Lakshan Dissanayake wrote:
nirjari patel wrote:
WILL IT THROW THIS EXCEPTION AT LAST OR NOT ?


I guess it won't. Garbage Collector clears allocated memory space for JVM.

Referring to above mentioned code, no, it won't throw any StackOverflowError. I would like to repeat Henry's question: Why do you think that there will be StackOverflowError (btw, its an error, not an exception)?

Whenever a new method is invoked, a new frame is added to method stack (and the frame is removed from the stack once the method is complete). In infinite while loop, there is no call to any method (technically, there is a call to println method - but as already mentioned, a new frame will be added during invocation of println, and the frame will be removed from stack after println is complete), and hence, there won't be any StackOverflowError.

Now, regarding garbage collection - all the references declared inside a loop are scoped only for that particular loop. If a new object is created in a loop, and that object does not have any reference whose scope can go beyond the loop, then that object gets eligible for GC immediately after the iteration in which the object was created. e.g.


Further, GC (or lack of it) is more related to OutOfMemoryError, and not to StackOverflowError. A code where a truckload of objects are created but never made eligible for GC would cause OutOfMemoryError. A StackOverflowError is typically caused during improper use of recursive method calls.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

Anayonkar Shivalkar wrote:
Referring to above mentioned code, no, it won't throw any StackOverflowError. I would like to repeat Henry's question: Why do you think that there will be StackOverflowError (btw, its an error, not an exception)?

Whenever a new method is invoked, a new frame is added to method stack (and the frame is removed from the stack once the method is complete). In infinite while loop, there is no call to any method (technically, there is a call to println method - but as already mentioned, a new frame will be added during invocation of println, and the frame will be removed from stack after println is complete), and hence, there won't be any StackOverflowError.

Now, regarding garbage collection - all the references declared inside a loop are scoped only for that particular loop. If a new object is created in a loop, and that object does not have any reference whose scope can go beyond the loop, then that object gets eligible for GC immediately after the iteration in which the object was created. e.g.


Further, GC (or lack of it) is more related to OutOfMemoryError, and not to StackOverflowError. A code where a truckload of objects are created but never made eligible for GC would cause OutOfMemoryError. A StackOverflowError is typically caused during improper use of recursive method calls.

I hope this helps.


Thanks buddy!
 
GeeCON Prague 2014
 
subject: DOes "while(true)" condition make a loop endless ?