How threads are executed in the memory?

前端 未结 2 1081
南旧
南旧 2020-12-12 01:40

I am learning about Thread in Java. I was tring to fetch which thread is running. But, I am not able to understand the order of the output.

Following is

相关标签:
2条回答
  • 2020-12-12 02:23

    Threads, by their nature, are concurrent to each other. This means, that two (or more) threads compete for the same resource (CPU) when they are executed at the same time, and CPU switches itself from executing one to another in a random-to-you (unpredictable) order. You can't and won't know to which execution path (Thread) your CPU and OS architecture will decide to jump. Moreover, in some languages, this may be a question of the OS architecture, in some - the question of CPU architecture only, and in some - question of both. It depends on how that language's architecture manages threads.

    Note, that even if two threads are parallel - i.e. they execute in parallel on two different cores - you still are unable to predict which core will execute instruction first.

    Because of the points above, you may be getting a different order of execution of your code, each time you run it.

    0 讨论(0)
  • 2020-12-12 02:25

    First .. please format your code for better visibility.Then use java naming convention.To find them out look here. It seems that you are a beginner in Threads and not understanding them in the first glance is completely OK.The unexpected order is related to OS scheduling and the concept of executing instructions and not methods concurrently, this mean that concurrent execution does not mean that run method will run concurrently, that means that everything (every instruction) in that method can/will run concurrently. Just keep learning Threads are (maybe) the most difficult area of Java but also the most interesting.

    0 讨论(0)
提交回复
热议问题