What's it called when multiple threads execute at the same time? Parallelism? Concurrency? šŸ¤” Is there a difference? (Spoiler: Yes!)

Let me explain in a few tweets, ripping off @pressron's #InsideJava blog post "On Parallelism and Concurrency". šŸ§µ

inside.java/2021/11/30/on-ā€¦
Parallelism:
Taking a task and splitting it up, so multiple CPUs can compute partial solutions in parallel to solve the task in less wall-clock time.

Concurrency:
Having a number of tasks that need to be arranged in a way that solves as many of them per $time_unit as possible.
Some parallelizable tasks:

* sorting an array
* inverting a matrix
* rendering graphics

Some examples for concurrency:

* processing incoming web requests
* making outbound calls to DB and/or other web services
* observe file system for changes
Parallel and concurrent problems have a lot in common, but also a number of distinctive properties:

* task origin and control
* resource use and coordination
* main metric
* use and number of threads

Let's juxtapose them.
Task origin and control:

In parallel computing, it's the algorithm / the solution that produces multiple tasks and dev is in control over their shape and number.
In concurrent computing, tasks emerge from the environment and are part of the problem.
Resource use and coordination:

Parallel tasks coordinate use of resources (e.g. CPU time or data structures).
Concurrent tasks compete over resources (e.g. I/O).

Main Metric:

Latency for parallelized tasks.
Throughput for concurrent tasks.
Use and number of threads:

In parallel computing, threads are an abstraction for CPU cores. Thread count is usually in the same order of magnitude as core count.

In concurrent computing, threads are an abstraction for tasks. Having one thread per task would be easiest.
The distinction isn't always clear cut (e.g. booting an app requires initializing plugins / latency-oriented [P] but thread per plugin/task [C]) and the styles can be nested (e.g. a web request [C] requires sorting a large array [P]), but it's important to differentiate them.
Java supports both approaches, e.g.:

* thread pools work for either
* parallel streams lean towards parallelism
* reactive leans toward concurrency

#ProjectLoom will greatly improve support for concurrent programming by allowing us to have one (virtual) thread per task.
Beyond that, Loom aims to introduce *structured* concurrency. More on that in the JEP draft (openjdk.java.net/jeps/8277129), the latest Newscast (), and an upcoming thread.

šŸ
Here's the thread I threatened. šŸ„ (I'll see myself out.)

ā€¢ ā€¢ ā€¢

Missing some Tweet in this thread? You can try to force a refresh
怀

Keep Current with Nicolai Parlog

Nicolai Parlog Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @nipafx

21 Dec
#ProjectLoom's virtual threads will make high performance in concurrent systems attainable with much simpler code. But Loom aims for even more and wants to make the code clearer and more robust by introducing *structured concurrency*.

Here's what that's all about. šŸ§µ
Important: This is about concurrency, not parallelism. See yesterday's thread šŸ‘‡šŸ¾ for a more detailed distinction, but the gist is that concurrency is about processing lots of tasks that the environment throws at your system at the same time, ideally with high throughput.
Concurrency code is often unstructured:

* splitting and joining concurrent subtasks in different methods/classes
* little support to compose error-handling, cancellation, ā€¦ across subtasks
* threading structure is opaque to the runtime, which only sees independent threads
* ā€¦
Read 9 tweets
10 Dec
I'm no security expert and don't know how ubiquitous this vulnerability is, but if you use Log4J 2.x, you should probably update to 2.15.x and read these:

logging.apache.org/log4j/2.x/secuā€¦
lunasec.io/docs/blog/log4ā€¦
Here's something else you can do until the updates are rolled out.
The other option until you've updated your dependency is this command line flag.
Read 4 tweets
3 Nov
From compact record constructors to pattern matching, from generic wildcards to chaining predicates and comparators, from jpackage to jlink - here are 11 #Java tricks handpicked from dev.java. šŸ§µ

(Musical version of this thread: )

0/11
A #record's *canonical* constructor has one arg per component but in its *compact* form, you don't have to list them. You also don't have to (in fact, can't) assign fields, just do the checks/reassignments you need, rest happens automatically.

More: dev.java/learn/using-reā€¦

1/11
Serialization works great with #records, no black magic needed! The guaranteed presence of constructor parameters and accessors makes serialization work with the object model and makes it easy to create reliable serializable records.

More: dev.java/learn/using-reā€¦

2/11
Read 12 tweets
16 Mar
Here are 11 improvements you get when updating to #Java16 later today: from records and type patterns to `Stream`-additions and Unix domain sockets, from creating installers to more performance and observability. šŸ§µšŸ‘‡šŸ¾

(Longer form with tons of links: nipafx.dev/java-16-guide/)
#1 Records

Express in a single line that a type is just a collection of data without need for encapsulation and let the compiler do the rest:

record Range(int low, int high) { }

That results in almost the same API as the attached class. *drops mic* Image
#2 Type Pattern Matching

This is actually two-for-one:

* first step into pattern matching
* type patterns with `instanceof`

With a type pattern you check whether a variable is of a certain type and, if so, create a new variable of that type, so you can use it without casting. Image
Read 14 tweets
14 Mar
"Towards better serialization!"

That's a guiding light of #ProjectAmber and record serialization is the first step. The Inside Java Podcast episode on that topic with Julia Boes and @chegar999 (inside.java/2021/03/08/podā€¦) gives great insight into how it achieves that. šŸ§µ

1/10
First, what's wrong with regular serialization? In short:

* extralinguistic mechanism (aka "magic")
* undermines encapsulation
* causes security issues
* hinders evolving code
* holds back new Java features

In long (and why it turned out that way), see attached thread.

2/10
"The magic is where the sin was" (@BrianGoetz) and so record serialization promises "what you see is what you get", making it:

1. easy to understand
2. no magic
3. more secure
4. more performant

3/10
Read 10 tweets
12 Mar
Do you dream of "value types" in Java? So do I! Hence I was thrilled to see that Project Valhalla is slowly coming out of exploration with two draft JEPs.

Here's what they currently propose. šŸ§µšŸ‘‡šŸ¾

(If you prefer video: https:/www.youtube.com/watch?v=WBvTilbh8S0&t=344s)

1/10
First, why value types? To bridge the divide between

* primitives (fast, no memory overhead) and
* reference types (abstraction, safety, identity)

As is, we sometimes have to choose between performance and design. And we often choose wrongly.

2/10
Draft JEP openjdk.java.net/jeps/8251554 proposes new terminology and JVM rules:

1. Interface `IdentityObject` (the boring part):

* for reference types
* called "identity classes"
* instances are "identity objects"

For identity classes / reference types everything stays as is.

3/10
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(