Some may say that you cant compare apples and oranges. That's true. (apples and oranges in here being Java and Erlang. From your preference pick which is which). But we can certainly put them into a salad, separately and compare the final taste.
Lets set some goals here.
Lets set some goals here.
- The main goal here is not to compare Java vs Erlang. It will be a separate issue and will need a separate approach.
- The goal will be to compare each ones concurrency aspects.
- Conduct the comparison to as deep as possible.
- If the comparison is too long, create another post and continue there. This is because Im free winging right now, so this may become lengthy. :)
Processes and threads
The terms process and thread can be interchangeable in software concurrency realms. Generally speaking process/thread is some component/things that are responsible to perform assigned tasks.
Generally a process has a memory space that is needed for its operations. For example lets say there is a process called A, is reading bytes from a socket. A's memory space may consists of the sockets identifier, if socket is writing data to a file, it needs to know that file descriptor, A may have some variables and data in his memory space.
A thread operates under a process. So a thread shares that process's memory. Everything in that process memory is accessible by the threads. Process can have multiple threads.
Good things: Threads are easy to create.
Why ?
A process needs its own memory space. So if you create 2 processes, somebody (your OS or JVM) has create/allocate/chunk another portion of memory. Since threads share the same memory as the process you don't need to create another memory chunk for each thread. Just create the thread and use it.
Bad things: Since multiple threads are accessing and operating on the same memory, they need to communicate and take turns when they access a common thing, lets say a variable/ file descriptor.
Java
Java follow processes and threads jargon. So in Java, we can create processes with ProcessBuilder (https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) objects and create threads with Thread (https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html) objects.
Java processes/threads are built by the JVM and has one to one mapping with OS processes and threads. And yes, Java has ways to communicate between threads when sharing memory. More later.
Erlang
Erlang does not has concept of threads. It only has processes which created with the Erang VM (EVM/ beam). Since these are not kernel level processes but VM level processes, are very very light weight and can spawn instantaneously. If using raw Erlang,we can use spawn(Module, Function, Args) (http://erlang.org/doc/getting_started/conc_prog.html) to create a proces. If we are using Erlang Open Telecom Platform we can use custom made very specific processes like gen_fsm, gen_server, gen_statem.
Erlang is a functional prog language and it has this principal called "Share Nothing". Since Erlang processes has no common memory, they can operate separately without any conflicts easily.
0 comments:
Post a Comment