Skip to content
Advertisement

java concurrency vs parallelism

I know the difference between the two clearly.

Concurrency is performing multiple tasks alternately, while parallelism is performing multiple tasks at once.

However, there is confusion in this code because a certain instructor has a different opinion than me.

Here is code:

public static void main(String[] args) throws InterruptedException {
    Thread t1 = new Thread(() -> System.out.println("T1"));
    Thread t2 = new Thread(() -> System.out.println("T2"));
    t1.start();
    t2.start();
    t1.join();
    t2.join();
}

I thought that running this code on multiple cores would run with parallelism, not concurrency, the instructor says concurrency.

If concurrency is not parallel, what is the reason?

Advertisement

Answer

First of all, this is more of a lexical problem than a programming problem. And perhaps one that doesn’t really apply to java.


One of the fundamental concepts in java, is that we try to write code platform independent. We don’t pre-optimize for specific hardware. The same code can run on any system no matter how many processors it has. There is a JIT (just-in-time) compiler, which will rewrite the code at-runtime to optimize it, once the hardware is known.

In java we have only very little control over the number of active cores.

And so, in other words, we don’t make the distinction between “parallelism”, “concurrency” or “multi-threading”. Those words are just used interchangeably.

Having said that, the official tutorial says that “concurrency” is even possible on a system with just 1 core. And what it means is that functions can get interrupted halfway, and resumed later on. And it happens so fast and often, that it really seems like threads are running completely parallel even when they run on the same core.

You often don’t know when/where that will happen. So, you need synchronize keywords and other locking strategies, even on a single-core system.


In the end, “parallel” and “concurrent” are just English words. “Concurrent”:

1 : operating or occurring at the same time. 2a : running parallel. b : convergent specifically : meeting or intersecting in a point. 3 : acting in conjunction.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement