简体   繁体   English

Java中的Stack Overflow和Collections-Java中的Stack实现

[英]Stack Overflow in Java with the Stack implementation in Collections-Java

I have a question which is related to the Stack implementation in the Collections framework in Java. 我有一个与Java的Collections框架中的Stack实现有关的问题。

  1. 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? 这是否意味着永远不会发生StackOverflowError并且最终Stack达到大小并且发生OutOfMemoryError
  2. 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? 从谷歌搜索中,我发现不赞成使用Vector类,因为它可以同步每个操作,正如Jon Skeet在此处指出的那样: Java Vector是否被弃用?

So, after this is there any real life scenario where I would use this Java class? 那么,在这之后,在现实生活中我将使用该Java类吗? 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. 有人可以提供现实生活中的情况/示例吗?

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. 首先,您不应该使用java.util.Stack ,就像您不应该使用Vector -它们都是遗留集合类,自Java 1.2版以来,它们已由DequeArrayList取代。 Note that Stack 's API documentation says: 请注意, Stack的API文档说:

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. Deque接口及其实现提供了一组更完整和一致的LIFO堆栈操作,应优先使用此类。

Note that StackOverflowError extends Error , not Exception or RuntimeException . 请注意, StackOverflowError扩展了Error ,而不是ExceptionRuntimeException Errors are only thrown if there's some internal error in the JVM - they are not normally thrown by Java classes. 仅在JVM中发生内部错误时才引发错误-Java类通常不会引发错误。

StackOverflowError is thrown only when the method call stack overflows; 仅当方法调用堆栈溢出时,才引发StackOverflowError it does not have anything to do with java.util.Stack . 它与java.util.Stack没有任何关系。

As you can see in the API documentation of StackOverflowError : 如您在StackOverflowError的API文档中所看到的:

Thrown when a stack overflow occurs because an application recurses too deeply. 由于应用程序递归过深而在堆栈溢出时抛出。

A stack, the data structure, which goes by java.util.Stack , can grow. 堆栈,即java.util.Stack的数据结构,可以增长。 The method call stack in Java, however, cannot. 但是,Java中的方法调用堆栈不能。 It thus can overflow, invoking a StackOverflowError . 因此,它可能会溢出,从而调用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. 这里在java.util.Stack和java方法调用堆栈之间有重要的区别。 One holds an array of generics, and the other is inherent to the JVM. 一个拥有一系列泛型,而另一个则是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. 我从未听说有人无意中溢出了JVM的堆栈,除非他们正在递归上工作。

Don't use Vector . 不要使用Vector There are much better classes out there. 那里有更好的课程。 You can also just use an ArrayList and keep its access synchronized . 您也可以只使用ArrayList保持其访问同步

StackOverflowError has nothing to do with java.util.Stack . StackOverflowErrorjava.util.Stack无关。 The java.util.Stack will grow as long as there is free memory, or OutOfMemoryError is thrown. 只要有可用内存,或者OutOfMemoryErrorjava.util.Stack就会增长。

In new applications, there is no need to use either Vector or Stack . 在新的应用程序中,无需使用VectorStack Use ArrayList or ArrayDeque . 使用ArrayListArrayDeque You rarely need thread safety provided by Vector , and if you do, use Collections.synchronizedList or LinkedBlockingDeque . 您很少需要Vector提供的线程安全性,如果需要,请使用Collections.synchronizedListLinkedBlockingDeque 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/ 但是,从HotSpot JVM 1.6开始,这不再是问题,请参见此处的示例: http : //www.ibm.com/developerworks/java/library/j-jtp10185/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM