Home > Mobile >  java concurrency vs parallelism
java concurrency vs parallelism

Time:10-23

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?

CodePudding user response:

Concurrency is when the execution of multiple tasks can overlap.

Parallelism is the actual parallel execution of tasks. So multiple tasks are running at the same time.

Example:

You need to get a new passport and work on a report.

1: No concurrency and no parallelism: You first get the new passport and then work on the report.

2: Concurrency and no parallelism: While you are in the lining waiting for your passport, you open your laptop and work on the report. As soon as it is your turn, you close the laptop and deal with the new passport. And later you continue work on the report. So the tasks are overlapping, but the tasks are not running at the same time.

3: Concurrency and parallelism: You ask someone else to get your passport while you work on the report. So 2 tasks are actually running at the same time.

About the example:

    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();
    }

It is obvious that t1/t2 are overlapping and hence they are concurrent. So there is concurrency.

If this would be run on an ancient laptop with just a single core and no hyperthreading, then there is no parallelism possible since only 1 thread at any moment can be running.

But on modern CPUs, both threads can run in parallel since there are multiple cores. So if the 2 threads are running at the same time, then there is also parallelism.

So there is certainly concurrency and potentially parallelism (depending on the hardware).

CodePudding user response:

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.

  • Related