Stack Overflow in Java with the Stack implementation in Collections

I have a question which is related to the Stack implementation in the Collections framework in Java.

  • I can see from the implementation that the size of the Stack can grow. Does this mean that a StackOverflowError can never occur and eventually the Stack reaches a size and an OutOfMemoryError occurs?
  • From googling I found that the Vector class is deprecated since it synchronizes each and every operation as Jon Skeet pointed out here: Is Java Vector deprecated?
  • So, after this is there any real life scenario where I would use this Java class? I don't want to synchronize on each and every operation and want to synchronize on a bunch of operations. Can somebody give a real life situation/example.


    A stack, the data structure, which goes by java.util.Stack , can grow. The method call stack in Java, however, cannot. It thus can overflow, invoking a StackOverflowError . This occurs primarily when dealing with recursion, but can occur elsewhere if you're not too careful.

    There is an important distinction here between java.util.Stack and the java method call stack. One holds an array of generics, and the other is inherent to the JVM. The latter is used when a method exits to return to the previous method.

    I've never heard of anyone unintentionally overflowing the JVM's stack, unless they were working on recursion.

    Don't use Vector . There are much better classes out there. You can also just use an ArrayList and keep its access synchronized.


    First of all, you shouldn't use java.util.Stack , just like you shouldn't use Vector - they are both legacy collection classes which were replaced by Deque and ArrayList since Java version 1.2. Note that Stack 's API documentation says:

    A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class.

    Note that StackOverflowError extends Error , not Exception or RuntimeException . Errors are only thrown if there's some internal error in the JVM - they are not normally thrown by Java classes.

    StackOverflowError is thrown only when the method call stack overflows; it does not have anything to do with java.util.Stack .

    As you can see in the API documentation of StackOverflowError :

    Thrown when a stack overflow occurs because an application recurses too deeply.


    StackOverflowError has nothing to do with java.util.Stack . The java.util.Stack will grow as long as there is free memory, or OutOfMemoryError is thrown.

    In new applications, there is no need to use either Vector or Stack . Use ArrayList or ArrayDeque . You rarely need thread safety provided by Vector , and if you do, use Collections.synchronizedList or LinkedBlockingDeque . However, since HotSpot JVM 1.6, this is not a problem any more, see for example here: http://www.ibm.com/developerworks/java/library/j-jtp10185/

    链接地址: http://www.djcxy.com/p/76146.html

    上一篇: 继承和重写同步的方法

    下一篇: Java中的堆栈溢出与集合中的堆栈实现