As far as I know, Threads share all of the memory segment except for stack. Let’s look example of this code.
class MyMain {
private int counter = 0;
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
counter++;
}
});
}
}
As you can see, I created a new thread inside of the main thread and pass counter
variable which is stored in the stack of main thread. So how can thread t1 have access to the stack of the main thread.