Is there a semantic difference between the terms call stack and thread stack, in Java multithreading?
Each thread has its own call stack, "call stack" and "thread stack" are the same thing. Calling it a "thread stack" just emphasizes that the call stack is specific to the thread.
Bill Venners calls this the Java stack:
When a new thread is launched, the Java virtual machine creates a new Java stack for the thread. As mentioned earlier, a Java stack stores a thread's state in discrete frames. The Java virtual machine only performs two operations directly on Java Stacks: it pushes and pops frames.
The method that is currently being executed by a thread is the thread's current method. The stack frame for the current method is the current frame. The class in which the current method is defined is called the current class, and the current class's constant pool is the current constant pool. As it executes a method, the Java virtual machine keeps track of the current class and current constant pool. When the virtual machine encounters instructions that operate on data stored in the stack frame, it performs those operations on the current frame.
When a thread invokes a Java method, the virtual machine creates and pushes a new frame onto the thread's Java stack. This new frame then becomes the current frame. As the method executes, it uses the frame to store parameters, local variables, intermediate computations, and other data.
A call stack is a stack data structure that stores information about the active subroutines of a computer program.
What you're calling a thread stackis what i assume is the private stack of a thread.
These two things are essentially the same. They are both stack data structures.
A thread's stack is used to store the location of function calls in order to allow return statements to return to the correct location
Since there usually is only one important call stack, it is what people refer to as the stack.
Here is information about the stack.
Here is information about Stack-based memory allocation.
Each thread has its own stack, each method call uses a new area of that stack. This means when a method calls itself (recursion), it will have a new set of local variables.
When FileWriter throws an IOException, the runtime system immediately stops executing the try block; method calls being executed are not completed. The runtime system then starts searching at the top of the method call stack for an appropriate exception handler.
In this example, when the IOException occurs, the FileWriter constructor is at the top of the call stack. However, the FileWriter constructor doesn't have an appropriate exception handler, so the runtime system checks the next method — the writeList method — in the method call stack. The writeList method has two exception handlers: one for IOException and one for IndexOutOfBoundsException.
Related
Please explain how JVM collect ThreadDump underhood.
I don't understand how it collectons stack traces of threads that are off-CPU (wait disk IO,Network,non-voluntary context switches).
For example, linux perf collect info only about on-CPU threads(which use CPU-cycles)
I'll take HotSpot JVM as an example.
The JVM maintains the list of all Java threads: for each thread it has a corresponding VM structure. A thread can be in one of the following states depending on its execution context (HotSpot knows the current state of each thread, because it's responsible for switching states):
in_Java - a thread is executing Java code, either in the interpreter or in a JIT-compiled method;
in_vm - a thread is inside a VM runtime function;
in_native - a thread is running a native method in JNI context;
there are also transitional states, but let's skip them for simplicity.
An off-cpu thread can have only
in_native state: all socket I/O, disk I/O, and otherwise blocking operations are performed only in native code;
in_vm state, when a thread is blocked on a VM mutex.
Whenever the JVM calls a native method or acquires a contended mutex, it stores the last Java frame pointer into the Thread structure.
Now the crucial part: HotSpot JVM obtains a thread dump only at a safepoint.
When you ask for a thread dump, the JVM requests a stop-the-world pause. All threads in in_Java state are stopped at the nearest safepoint, where the JVM knows how to walk the stack.
Threads in in_native state are not stopped, but they don't need to. HotSpot knows their last Java frame, because the pointer is stored in a Thread structure. Knowing the top Java frame, the JVM can find its caller, then the caller of the caller, and so on.
What important here is that the Java part of the stack is "frozen", no matter what the native method does. The top part of the stack (native) can change back and forth, while the bottom part (Java) remains immutable. It cannot change, since the JVM checks for a pending safepoint operation on every switch from in_native to in_Java: if a native method returns, and the VM is currently running a stop-the-world operation, current thread blocks until the operation ends.
So, getting a thread dump involves
Stopping all in_Java and in_vm threads at a safepoint;
Walking through the global list of threads maintained by the JVM;
If a thread is running native method, its top Java frame is stored in a thread structure; if a thread is running Java code, its top frame corresponds to the currently executing Java method.
Each frame has a link to the previous frame, so given the top frame, the JVM can construct the whole stack trace to the bottom.
I have a download operation code that look's like this
while(true){
if(target.flagStop){
break;
}else{
x=target.check();
}
len=in.read(buff,0,min(BUFFER_SIZE,x));
out.write(buff,0,len);
target.position+=len;
}
which flagStop is a volatile boolean
and position is a non volatile long value
and inside a check() method I have a synchronized block
long check(){
//some code here
synchronized(aLock){
//some code here
return something;
}
}
I update(write access) position only in this thread(only care about this to be exactly have lastest updated) but also some reads from different threads occurs, im my case thease are just for monitoring purposes
so a few bytes lower than I expected it does'nt matter vs than declarimg value as volatile which that costs on performance on my main purpose
I know for a CPU instruction to be completed data comes to CPU register after computation progress result will come back to memory
which
- if that variable declared as volatile the result immediately will be written to main memory (not cached anymore)
- otherwise this will be stored in thread cache memory after that in future this value will be written to main memory(write to main memory from cache, time can't be determined (this can be immediately or has a delay no one knows) in my case my question is about this situation that value is not volatile and only in one thread
according to an answer from a dear User in StackOverflow in here when we enter a synchronized block first of all
(case 1): we have a read operation from main memory (mentioned as read barrier)
and at the end of synchronized block
(case 2): we have write operation to main memory (mentioned as write barrier)
I know about case 2
all the modified thread cache variables will be written into main memory
but something that maybe I'm thinking wrong is that in case 1:
we have a read operation from main memory which that overrides thread's cache with version that stored in main memory.(main -> cache)
As I mentioned earlier my position value is not volatile (so have not directly access read/write to main memory use cached value instead) and if I enter to a synchronized block which that case 1 occurs (since that possible ,newer position value from thread's cache have not yet have chance to writes its value to main memory) and overrides main memory(possibly older one) version of position into thread cache(i.e. destroy newer one by overriding older value that retrieved by synchronization monitor enter operation)
is that really I'm thinking true?
and I must declare position as a volatile or not?
and tell me if I'm wrong that what's happening in thread cache at monitor enter(or case 1 that I mentioned before)
Thanks in advance for your guidance.
Part of what you are looking for:
https://docs.oracle.com/javase/specs/jls/se14/html/jls-17.html#jls-17.4.1
Memory that can be shared between threads is called shared memory or
heap memory.
All instance fields, static fields, and array elements are stored in
heap memory. In this chapter, we use the term variable to refer to
both fields and array elements.
Local variables (§14.4), formal method parameters (§8.4.1), and
exception handler parameters (§14.20) are never shared between threads
and are unaffected by the memory model.
"Unaffected by" here means that they don't need to be synchronized. As long as only one thread sees a variable, it's always fine.
This also helps:
https://docs.oracle.com/javase/specs/jls/se14/html/jls-17.html#jls-17.4.7
The execution obeys intra-thread consistency.
For each thread t, the actions performed by t in A [actions by that thread] are the same as
would be generated by that thread in program-order in isolation, with
each write w writing the value V(w), given that each read r sees the
value V(W(r)). Values seen by each read are determined by the memory
model. The program order given must reflect the program order in which
the actions would be performed according to the intra-thread semantics
of P.
Actions means both reads and writes. So your variable position is not allowed to be updated with some strange values because of synchronization. The reads and writes within a single thread of execution happens in the same order as the program statements specify. The system will not pull strange reads or writes out of cache or main memory out of order.
I was reading article about Internal Java Memory Model.
There is one point I want to ask about :
Each thread running in the Java virtual machine has its own thread stack. The thread stack contains information about what methods the thread has called to reach the current point of execution.
Why each thread needs to save information about what methods has been executed(!) ? If it's related to context-switching then (if I'm not wrong) thread must save the information about method which is currently being executed.
What is actual need for save already executed method's information?
This is referring to the currently active methods. Note that there can be several methods in a thread active at the same time (A calls B calls C, ...). The stack does not contain information about methods that have already completed.
I think rephrasing this paragraph makes it clearer and easier to understand:
Each thread running in the Java virtual machine allocates some memory for its call stack. The call stack contains information about what methods the thread has called to reach the current point of execution.
On the JVM, I run a thread and at some point I block it. If I persist the JVM thread stack at this point, and all the objects I explicitly created in my code that it refers to (I assume they are all serializable), will it be feasible to use this data to resume the thread on another JVM?
Are there any frameworks/libraries out there that can help me or get me closer to doing such a thing?
When I say something like:
Thread t1 = new Thread();
does it create it on a heap or a stack?
There is no way to allocate objects on the stack in Java.
The stack can only hold references and primitives, and only for local variables.
Note that starting a thread will create a new stack for that thread.
Thread t1 = new Thread();
tl;dr This allocates object i.e. t1 in heap.
As each new thread comes into existence, it gets its own pc register (program counter) and Java stack. If the thread is executing a Java method (not a native method), the value of the pc register indicates the next instruction to execute. A thread's Java stack stores the state of Java (not native) method invocations for the thread. The state of a Java method invocation includes its local variables, the parameters with which it was invoked, its return value (if any), and intermediate calculations. The state of native method invocations is stored in an implementation-dependent way in native method stacks, as well as possibly in registers or other implementation-dependent memory areas.
The Java stack is composed of stack frames (or frames). A stack frame contains the state of one Java method invocation. When a thread invokes a method, the Java virtual machine pushes a new frame onto that thread's Java stack. When the method completes, the virtual machine pops and discards the frame for that method.
The Java virtual machine has no registers to hold intermediate data values. The instruction set uses the Java stack for storage of intermediate data values.
Figure shows a snapshot of a virtual machine instance in which three threads are executing. At the instant of the snapshot, threads one and two are executing Java methods. Thread three is executing a native method. It also shows of the memory areas the Java virtual machine creates for each thread, these areas are private to the owning thread. No thread can access the pc register or Java stack of another thread.
In Java 8, using Escape Analysis objects can be created on the stack. This occurs when an object is detected as not escaping the current method (after inlining has been performed) Note: this optimisation is available in Java 7, but I don't think it worked as well.
However, as soon as you call start() it will escape the current method so it must be placed on the heap.
When I say something like:
Thread t1 = new Thread();
does it create it on a heap or a stack?
It could place it on the stack, provided you don't use it to create a real thread. i.e. if you so
Thread t1 = new Thread(runnable);
t1.start();
It has to place it on the heap.