concept stack in category java

appears as: stack, stack, stacks, stacks
The Java Module System

This is an excerpt from Manning's book The Java Module System.

9.2.3 Inside-out modularization: If a project is in the middle of the stack

Most libraries and, particularly, frameworks are neither at the bottom nor at the top of the stack—what are they to do? They modularize inside-out. This process has a bit of bottom-up (section 9.2.1) in it because releasing modular JARs doesn’t force users to use them as modules. Other than that, it works like top-down (section 9.2.2), with one important difference: you’re planning to publish the modular JARs you built. See figure 9.5 for an example of an inside-out modularization.

Figure 9.5 If automatic modules are used carefully, libraries and frameworks in the middle of the stack can publish modular JARs even though their dependencies and their users may still be plain JARs, thus modularizing the ecosystem from the inside out.

c09_05.png
Functional Programming in Java: How functional techniques improve your Java programs

This is an excerpt from Manning's book Functional Programming in Java: How functional techniques improve your Java programs.

As you may know, Java is limited in terms of recursion. Methods can call themselves recursively, but this implies that the state of the computation is pushed on the stack for each recursive call, until a terminal condition is reached, at which time all preceding states of the computation are popped out of the stack, one after the other, and evaluated. The size of the stack can be configured, but all threads will use the same size. The default size varies according to the implementation of Java, from 320 KB for a 32-bit version to 1,064 KB for a 64-bit implementation, both of which are very small compared to the size of the heap, where objects are stored. The end result is that the number of recursive steps is limited.

Determining how many recursive steps Java can handle is difficult, because it depends on the size of the data that’s pushed on the stack, and also on the state of the stack when the recursive process starts. In general, Java can handle about 5,000 to 6,000 steps.

The previous chapter introduced powerful methods and functions, but some shouldn’t be used in production because they can overflow the stack and crash the application (or at least the thread in which they’re called). These “dangerous” methods and functions are mainly explicitly recursive, but not always. You’ve seen that composing functions can also overflow the stack, and this can occur even with nonrecursive functions, although this isn’t common.

In this chapter, you’ll learn how to turn stack-based functions into heap-based functions. This is necessary because the stack is a limited memory area. For recursive functions to be safe, you have to implement them in such a way that they use the heap (the main memory area) instead of the limited stack space. To understand the problem completely, you must first understand the difference between recursion and corecursion.

To understand what’s happening, you must look at how Java handles method calls. When a method is called, Java suspends what it’s currently doing and pushes the environment on the stack to make a place for executing the called method. When this method returns, Java pops the stack to restore the environment and resume program execution. If you call one method after another, the stack always holds at most one of these method call environments.

But methods aren’t composed only by calling them one after the other. Methods call methods. If method1 calls method2 as part of its implementation, Java again suspends the method1 execution, pushes the current environment on the stack, and starts executing method2. When method2 returns, Java pops the last pushed environment from the stack and resumes execution (of method1 in this case). When method1 completes, Java again pops the last environment from the stack and resumes what it was doing before calling this method.

Method calls may be deeply nested, and this nesting depth does have a limit, which is the size of the stack. In current situations, the limit is somewhere around a few thousand levels, and it’s possible to increase this limit by configuring the stack size. But because the same stack size is used for all threads, increasing the stack size generally wastes space. The default stack size varies from 320 KB to 1024 KB, depending on the version of Java and the system used. For a 64-bit Java 8 program with minimal stack usage, the maximum number of nested method calls is about 7,000. Generally, you won’t need more, except in specific cases. One such case is recursive method calls.

Struts 2 in Action

This is an excerpt from Manning's book Struts 2 in Action.

You may have noticed, while studying figure 1.4, that there is a stack of interceptors in front of the action. The invocation of the action must travel through this stack. This is a key part of the Struts 2 framework. We’ll devote an entire chapter to this important component later in the book. At this time, it is enough to understand that most every action will have a stack of interceptors associated with it. These interceptors are invoked both before and after the action, though we should note that they actually fire after the result has executed. Interceptors don’t necessarily have to do something both times they fire, but they do have the opportunity. Some interceptors only do work before the action has been executed, and others only do work afterward. The important thing is that the interceptor allows common, cross-cutting tasks to be defined in clean, reusable components that you can keep separate from your action code.

No matter whether we call the task cross-cutting or preprocessing, the conceptual mechanics of interceptors are clear. Instead of having a simple controller invoking an action directly, we now have a component that sits between the controller and the action. In Struts 2, no action is invoked in isolation. The invocation of an action is a layered process that always includes the execution of a stack of interceptors prior to and after the actual execution of the action itself. Rather than invoke the action’s execute() method directly, the framework creates an object called an ActionInvocation that encapsulates the action and all of the interceptors that have been configured to fire before and after that action executes. Figure 4.1 illustrates the encapsulation of the entire action execution process in the ActionInvocation class.

Figure 4.1. ActionInvocation encapsulates the execution of an action with its associated interceptors and results.

As you can see in figure 4.1, the invocation of an action must first travel through the stack of interceptors associated with that action. Here we’ve presented a simplified version of the defaultStack. The defaultStack includes such tasks as file uploading and transferring request parameters onto our action. Figure 4.1 represents the normal workflow; none of the interceptors have diverted the invocation. This action will ultimately execute and return a control string that selects the appropriate result. After the result executes, each of the interceptors, in reverse order, gets a chance to do some postprocessing work. As we’ll see, the interceptors have access to the action and other contextual values. This allows them to be aware of what’s happening in the processing. For instance, they can examine the control string returned from the action to see what result was chosen.

One of the powerful functional aspects of interceptors is their ability to alter the workflow of the invocation. As we noted, figure 4.1 depicts an instance where none of the interceptors has intervened in the workflow, thus allowing the action to execute and determine which result should render the view. Sometimes, one of the interceptors will determine that the action shouldn’t execute. In these cases, the interceptor can halt the workflow by itself returning a control string. Take the workflow interceptor, for example. As we’ve seen, this interceptor does two things. First, it invokes the validate() method on the action, if the action has implemented the Validateable interface. Next, it checks for the presence of error messages on the action. If errors are present, it returns a control string and, thus, stops further execution. The action will never fire. The next interceptor in the stack won’t even be invoked. By returning the control string itself, the interceptor causes control to return back up the chain, giving each interceptor above the chance to do some postprocessing. Finally, the result that matches the returned control string will render the view. In the case of the workflow interceptor that has found error messages on the action, the control string is "input", which typically maps back to the form page that submitted the invalid data.

4.3.5. Built-in stacks

The Struts 2 framework comes with many built-in stacks that provide convenient arrangements of the built-in interceptors. We’ve been using one of these, the defaultStack, for all of our Struts 2 Portfolio packages. We inherit all of this, and other built-in stacks, just by having our packages extend the struts-default package defined in struts-default.xml. To make things simple, we recommend that you also use the defaultStack unless you have a clear imperative to do otherwise. Most of the other built-in stacks you could use are just pared-down versions of the defaultStack. This paring down is not done to make more efficient versions of the stacks by eliminating unnecessary interceptors. Rather, the smaller stacks are meant to be modular building blocks for building larger ones. If you find yourself building your own stacks, try to use these modular pieces to help simplify your task. Still, you might ask, why do we need the scopedmodelDriven interceptor in the stack if we aren’t using it? Isn’t this a performance hit? We think it isn’t. So far, it seems that unused interceptors don’t affect performance that much. Additionally, messing with the interceptors can be the fastest way to introduce debugging complexity. Ultimately, we always recommend using the built-in path of least resistance as long as possible—which means the defaultStack. While Struts 2 is flexible, it’s also meant to perform well and be useful right out of the box.