开发者

Multi-Core/Concurrent Programming and .NET/Java

开发者 https://www.devze.com 2023-03-28 23:33 出处:网络
I often hear about other languages promoted as being more suitable for multi-core/concurrent programming e.g. Clojure, Scala, Erlang etc. but I\'m a little confused about why I need to worry about mul

I often hear about other languages promoted as being more suitable for multi-core/concurrent programming e.g. Clojure, Scala, Erlang etc. but I'm a little confused about why I need to worry about multiple cores, shouldn't the Java/.NET VM handle that automatically and开发者_如何学Python if not, what are the reasons behind it?

Is it because those languages mentioned are functional and have some intrinsic advantage over non-functional languages?


The reason you need to care is that processors are generally speaking not getting any faster. Instead more of them are being added to computers in the form of additional cores. In order for a program to take advantage of the extra processors it generally speaking must use multiple threads to do so. Java, and most other languages, will not automatically use more threads in your program. This is something you need to manually do.

Some people prefer functional style languages like Scala, Erlang and F# to non-functional for multi-threaded programming. Functional languages tend to be at least shallowly immutable by default and hence in theory are easier to work with in multi-threaded situations.

Here is a very good article on this subject

  • The Free Lunch is Over


Functional languages have the intrinsic advantage that most functions/methods call are idempotent and that they typically use "immutability" a lot.

Doing concurrency when using immutable "stuff" is wwaayy easier than when using mutable "stuff".

But anyway: you need to worry about concurrency because we're going to CPU with more and more cores and not faster and faster clocks. Java / C# do not automagically make your program concurrents: you still have to do the hard work yourself.


In the case of normal imperative languages, by default no, you do not get much help from the platform. No compiler is clever enough to parallelize normal imperative code.

There are however various help libraries that can help you on different platforms. For example the Task Parallel library in .NET allows you to do this for example:

Parallel.ForEach(files, file => 
{
   Process(file);
});

Pure functional languages always have the benefit of minimal shared state which means that such code is more easily parallelized.


I never did program functional languages, but they are supposed to be easier for coding concurrently since the states do not change (or do not change much), so that you can have the same object on two threads at once. If you have the same object on two threads at once in Java, you have to be very careful and use some special synchronization constructs such that the two threads can "see" the object in the same state when it is necessary.

The current problem in programming is that everybody is used to doing things sequentially, but the hardware is moving multi-core. There has to be a complete paradigm shift in the way we code and think about code. Right now, coding for multi-core in Java or C# is basically just sequential coding with hacks to make it parallelizable. Will functional programming turn out to be the required paradigm shift? I don't know.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号