Low-Level Concurrency in Java

Low-Level Concurrency in Java

Higher level constructs like ThreadPoolExecutor, CompletableFuture, Latch etc. are intentionally omitted.

Java is a multi-threaded language. Since the beginning of the language it had first class support of multiple threads for our concurrency needs. After that with every release of Java the multi-thread model has been simpler and more accessible to users. From ThreadPoolExecutorFuture in Java 5 to CompletableFutureForkJoin framework etc in Java 8. Also powerful third-party frameworks like RxJava evolved in the wild.

Though these abstractions make concurrency more available to the average user, one still should learn about the nuts and bolts of the language (and JVM). It will help clear the picture of concurrency and appreciate the skillfully designed concurrency abstractions we use.

Higher level constructs like ThreadPoolExecutorCompletableFutureLatchetc. are intentionally omitted.

THREAD

A thread is a thread of execution in a program. You can run as many threads in parallel as many CPU cores you have. Although there can be hundreds of them in the waiting state. A thread represents a system thread as provided by the underlying OS (in most JVM implementations). Just like processes in an OS, threads are scheduled and have a life-cycle.

A thread can be created by creating a thread object and passing a runnableto it. Bear in mind creating a runnable does not create a thread. If you only create a runnable and run it directly it will just be executed by the current thread.

Threads can also be set daemon, meaning JVM can exit while these threads are running.

public class SimpleThreadExample {
        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("This is " + Thread.currentThread().getName());
                }
            }).start();
            System.out.println("And this is "+Thread.currentThread().getName());
        }
    }

multithreading concurrency java

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Multithreading Example | Multithreading in Java

Java Multithreading Tutorial With Example | Multithreading in Java. Multithreading is a process of executing multiple threads simultaneously.

6 Best Books to Learn Multithreading and Concurrency in Java

These are the best books to learn multi-threading and concurrency in Java for both beginners and experienced Java programmers and developers.

Java Concurrency

This article describes what is concurrent programming and what is the main goal of using it.

Learning by Doing: How to Learn Java Basics by Building Your Own Project

Check out some ideas for sample projects that can help you learn Java, and programming in general, in a fun and engaging way.