Why do threads share the heap space?

前端 未结 8 772
时光取名叫无心
时光取名叫无心 2020-12-23 10:12

Threads each have their own stack, but they share a common heap.

Its clear to everyone that stack is for local/method variables & heap is for instance/class vari

8条回答
  •  悲哀的现实
    2020-12-23 11:06

    In a multi-threaded application each thread will have its own stack but will share the same heap. This is why care should be taken in your code to avoid any concurrent access issues in the heap space. The stack is threadsafe (each thread will have its own stack) but the heap is not threadsafe unless guarded with synchronisation through your code.

提交回复
热议问题