Reputation: 45
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?
Upvotes: 1
Views: 1241
Reputation: 116848
Concurrency is performing multiple tasks alternately, while parallelism is performing multiple tasks at once.
I've been doing thread programming for a couple decades now and I've never heard of this distinction. The internet says that concurrency is any tasks that can run simultaneously (at the same time) in an application while parallelism is when you run the same task multiple times simultaneously. So multiple copies of the same task running concurrently is parallelism. In either case, I wouldn't argue semantics – especially with a professor :-).
Let's look at the code:
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.
It certainly is concurrent. The 2 threads can (although they may not) run at the same time. For the record, it could be that t1
or t2
might start running and might finish before the other gets started because of thread race conditions. Another wrinkle is that System.out
is a synchronized
class meaning that the 2nd thread executing a println(...)
statement will be blocked by the first so the output will never be printed concurrently. But I nit pick.
Using the internet definition of concurrency and parallelism, I would say that it is not parallelism since it is not the same job. If you executed the same Runnable
in 2 different threads then it would be. If you changed the code to be the following, again using the internet definition, then it would be parallelism (as well as concurrency):
Runnable job = () -> System.out.println("T" + Thread.currentThread().getId());
Thread t1 = new Thread(job);
Thread t2 = new Thread(job);
...
Hope this helps.
Upvotes: 0
Reputation: 11307
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.
No concurrency and no parallelism: You first get the new passport and then work on the report.
Concurrency and no parallelism: While you are in the line 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.
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).
Upvotes: 8
Reputation: 24710
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.
Upvotes: 2