Hello!

Started learning streams. The author of the course, which I pass, gave an example of working with static methods with multithreading. He noted that although the static method usually exists in a single copy (since it belongs to the entire object), when this method is executed in two threads, a copy of the method is created for each of them; the author notes that the word “copy” is used for a simplified understanding in this educational material, after which I quote:

In fact, very tricky processes are occurring - such as with the local storage of a stream while preserving intermediate register calculations - since IT IS EVERYTHING EACH OF THE STATIC METHOD. But thanks to these complex processes and mechanisms, we have the illusion that the static method is being copied. ... But we, as high-level programmers, are not interested.

What are these mechanisms? How are they used to create the illusion of a copy of the static method? And what have the “flow storage while keeping intermediate register calculations”? What is this storage?

At another forum, we noticed that the author had in mind that neither the usual methods of classes nor the static ones are copied.

Perhaps it meant a stack that the system creates for each thread; but the stack is not a copy. Then I don’t know what the author of the course meant - he said that it is the static methods that have some mechanisms that create the illusion of copying them.

If the author is still wrong, I ask you to define the concepts from the quotation and explain what the author wanted to say. Thank you in advance!

  • one
    "they want to show education ..." Why say that the method is copied if it is not copied? All this can be explained using the concept of "stack frame". Everyone tongue-tied thinks he can teach. - Igor
  • 2
    Nothing is copied anywhere. There is no illusion of copying. There is no tricky mechanism specifically for static methods. It is just that each thread performs the method by itself, without looking at the other threads. - PashaPash
  • one
    what just do not know in "these are your Internet." I'll tell you more, what VladD wrote in the answer applies to all methods and even properties (not to be confused with fields). Non-static ones are not copied anywhere either, just unlike static ones, you cannot call them without specifying a specific object. - rdorn
  • I was also told that this topic concerns this Wikipedia article :. Context switch (in multi-tasking OSs and environments) - the process of stopping a processor from performing one task (process, thread, thread) while preserving all the necessary information and state necessary for the subsequent continuation from the interrupted place, and restoring and loading the task to which the processor goes. - Is it so? - BadCatss

1 answer 1

Think of a method like this: a method is an instruction manual, such as a recipe. That the method is executed means that the thread reads the instructions and executes them.

It is clear that nothing prevents two threads from simultaneously doing this.

  • I was also told that this topic concerns this Wikipedia article :. Context switch (in multi-tasking OSs and environments) - the process of stopping a processor from performing one task (process, thread, thread) while preserving all the necessary information and state necessary for the subsequent continuation from the interrupted place, and restoring and loading the task to which the processor goes. - BadCatss
  • 3
    @BadCats: Context switching is how execution proceeds from thread to thread, needed for the case when there are more threads than the processor cores (or all system processors, if there are a lot of them). That is, almost always, because usually you have hundreds of processes, and each has several threads. - VladD