I've been reading about the topic and I'm a bit confused about multi-threading and parallelism; I've read this question
"How are threads distributed between CORES in a multithreaded system? Lets say I have a program that creates 6 threads. My system has 3 CORES. In this case will the threads be distributed between the 3 COREs or will all the threads execute on the same CORE?" - physical CPU with 3 logical cores
The answer to the question suggests that I have to tell the OS which core executes what, is this a universal truth when multithreading in a language like Java or C#?
In a scenario where I don't specify which core does what can I achieve parallelism in a multithreaded program written in languages like Java or C#?
I've been learning some erlang and I've reached the topic of spawning processes, when these processes are spawned; does erlang tell the OS to assign the different processes to different cores? I saw this code from Learn you some erlang, this spawns 10 processes and each will print out a number but why use timer:sleep ? isn't that just making the program look parallel when instead it's just making something stop so something else can run (like in concurrency) ?
4> G = fun(X) -> timer:sleep(10), io:format("~p~n", [X]) end.
#Fun<erl_eval.6.13229925>
5> [spawn(fun() -> G(X) end) || X <- lists:seq(1,10)].
I implemented this in java and got a similar result, I created 6 threads and each "thread" has a loop where it prints the thread name then a number then sleeps for 10 milliseconds.
public class ThreadTest {
public static void main(String args[]) {
Thread t1 = new Thread(new Thread2());
Thread t2 = new Thread(new Thread3());
Thread t3 = new Thread(new Thread4());
Thread t4 = new Thread(new Thread5());
Thread t5 = new Thread(new Thread6());
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
Looking at both programs are they executing things concurrently or in parallel ? I also realise that computers are fast but even if I don't make the java program sleep it will just print the things one after another. With erlang, however, if I remove sleep it sometimes prints out a whole lot of the numbers then prints the number of processes and continues counting or it prints out all numbers then the process list last.
In reference to the question above, is java doing things on one core concurrently (with context switching) or is it utilising more cores and doing things in parallel but just too fast to give me random results ? (without sleep)
Is erlang using more cores and doing things in parallel since it sometimes prints out the process list in the middle of counting ? (without sleep)
Note: I have purposefully left out the code for the other threads and just thought it'd be better to explain what the classes do.