Java Reference
In-Depth Information
When the version of fct returned by memo is called, it will access the values
of parm, fct,andans, which are used in its definition. After memo returns, its
frame must be preserved since that frame contains parm, fct and answithin it.
In general, when a function is created or manipulated, we must maintain
a pair of pointers. One is to the machine instructions that implement the
function, and the other is to the frame (or frames) that represent the function's
execution environment . This pair of pointers is called a closure .Notealsothat
when functions are first-class objects, a frame corresponding to a call may
be accessed after the call terminates. This means frames can not be routinely
allocated on the runtime stack. Instead, they may be allocated in the heap and
garbage-collected, just like user-created data. Intuitively this may seem to be
ine
cient, but Appel [App96] has shown that in some circumstances heap
allocation of frames can be faster than stack allocation.
Cactus Stacks Many programming languages allow the concurrent execu-
tion of more than one computation in the same program. Units of concurrent
execution are sometimes called tasks , processes ,or threads . In some cases a new
system-level process is created (as in the case of fork in C). Because significant
operating systemoverhead is involved, such processes are called heavyweight
processes . A less expensive alternative is to execute several threads of con-
trol in a single system-level process. Because much less state is involved,
computations that execute concurrently in a single system process are called
lightweight processes .
A good example of lightweight processes are threads in Java. As illustrated
below, a Java program may initiate several calls to methods that will execute
simultaneously:
public static void main (String args[]) {
new AudioThread("Audio").start();
new VideoThread("Video").start();
}
Here, two instances of Thread subclasses are started, and each executes con-
currently with the other. One thread might implement the audio portion of an
application, while the other implements the video portion.
Since each thread can initiate its own sequence of calls (and possibly start
more threads), all the resulting frames cannot be pushed on a single runtime
stack (the exact order in which threads execute is unpredictable). Instead, each
thread gets its own stack segment in which frames it creates may be pushed.
This stack structure is sometimes called a cactus stack , since it is reminiscent of
the saguaro cactus, which sends out arms from the main trunk and from other
arms. It is important that the thread handler be designed so that segments are
 
Search WWH ::




Custom Search