Erlang Processes vs Java Threads

后端 未结 6 1442
别那么骄傲
别那么骄傲 2020-11-29 21:18

I am reading \"Elixir in Action\" book by Saša Jurić, and in the first chapter it says:

Erlang processes are completely isolated from each other. They

6条回答
  •  星月不相逢
    2020-11-29 21:43

    Isn't that true for Java threads as well? I mean when Java thread crashes, it too does not crash other threads

    Yes and No. I explain:

    • Referring to shared memory: Different threads in a Java process share the whole heap, therefore threads can interact in a huge number of planned and unplanned ways. However objects in the stack (e.g. a context you pass down to called method) or a ThreadLocal are their own thread's (unless they start sharing references).

    • Crashing: If a thread crashes in Java (a Throwable is propagated into Thread.run(), or something gets looped or blocked), that mishap might not affect other threads (e.g. a pool of connections in a server will continue to operate). However as different threads interact. Other threads will easily get stranded if one of them ends abnormally (e.g. one thread trying to read from an empty pipe from another thread which did not close its end). So unless the developers are highly paranoid careful, it is very likely that side effects will occur.

    I doubt that any other paradigm intends threads to operate as totally independent islands. They must share information and coordinate somehow. And then there will be the chance to mess things up. It is just they will take a more defensive approach that "gives you less rope to hang yourself" (same idiom as with pointers).

提交回复
热议问题