ArticleslgStudy

computer science

Java concurrency

Java concurrency is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Java concurrency rather than just read about it. In short: The Java programming language and the Java virtual machine (JVM) are designed to support concurrent programming. All execution takes place in the context of threads.

Key takeaways

  • Java concurrency belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Java concurrency to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Java concurrency from memory before moving on to harder problems.

Reference excerpt

The Java programming language and the Java virtual machine (JVM) are designed to support concurrent programming. All execution takes place in the context of threads. Objects and resources can be accessed by many separate threads, with each thread with its own path of execution, but with access to any object in the program. Read and write access to objects must be properly coordinated (or "synchronized") between threads. Thread synchronization ensures that objects are modified by only one thread at a time and prevents threads from accessing partially updated objects during modification by another thread.

Processes and threads Most implementations of the Java virtual machine run as a single process. In the Java programming language, concurrent programming is primarily concerned with threads (also called lightweight processes). Multiple processes can only be realized with multiple JVMs.

Thread objects Threads share the process' resources, including memory and open files. This makes for efficient, but potentially problematic, communication. Every application has at least one thread called the main thread. The main thread has the ability to create additional threads as java.lang.Runnable or java.util.concurrent.Callable objects. The java.lang.Callable interface is similar to java.lang.Runnable in that both are designed for classes whose instances are potentially executed by another thread. A java.lang.Runnable, however, does not return a result and cannot throw a checked exception. Each thread can be scheduled on a different CPU core or use time-slicing on a single hardware processor, or time-slicing on many hardware processors. There is no general solution to how Java threads are mapped to native OS threads. Every JVM implementation can do this differently. Each thread is associated with an instance of the class java.lang.Thread. Threads can be managed either by directly using the java.lang.Thread objects, or indirectly by using abstract mechanisms such as java.util.concurrent.Executors or task classes. Java provides the following task classes:

java.util.concurrent.ForkJoinTask, and its descendants: java.util.concurrent.CountedCompleter java.util.concurrent.RecursiveAction (extends ForkJoinTask<Void>) java.util.concurrent.RecursiveTask java.util.concurrent.FutureTask javafx.concurrent.Task<V> (from JavaFX) As part of Project Loom in Java 21, virtual threads were introduced, which allow for normal, blocking, synchronous code while scaling to millions of tasks. The classic Java thread is called a "platform thread", corresponding roughly to an OS thread, but Java virtual threads are lightweight and managed by the JVM, scheduled onto a small pool of real OS threads. Virtual threads are still java.lang.Thread, but are created with factory methods like ofVirtual(). Although the code is still synchronous and blocking, the physical OS threads are asynchronous; upon stopping to wait, the JVM detaches the code from the physical thread, running other tasks until the OS sends an asynchronous event notification to pick up the code where it left off to finish.

Thread starting A thread may be started through providing a java.lang.Runnable object:

Or, through inheritance from java.lang.Thread:

Interrupts An interrupt tells a thread that it should stop what it is doing and do something else. A thread sends an interrupt by invoking interrupt() on a java.lang.Thread for the thread to be interrupted. The interrupt mechanism is implemented using an internal flag marking its interruption status, which is set upon a call to interrupt(). Any method that exits by throwing a java.lang.InterruptedException clears the interrupted status when it does so, but there is the possibility that the interrupted status may immediately be set again, by another thread calling interrupt(). Historically, threads could be stopped immediately using the stop() method; upon calling this, a java.lang.ThreadDeath would be thrown, which clears the thread. This method was dangerous as it could leave data in a corrupted state, and was eventually removed in Java 20.

Joins The java.lang.Thread#join() method allows a java.lang.Thread to wait for the completion of another.

Exceptions Uncaught exceptions thrown by code will terminate the thread. The main thread prints exceptions to the console, but user-created threads need a handler registered to do so (see java.lang.Thread.UncaughtExceptionHandler).

Memory model The Java memory model describes how threads in the Java programming language interact through memory. On modern platforms, code is frequently not executed in the order it was written. It is reordered by the compiler, the processor and the memory subsystem for performance reasons. There is no guarantee of linearizability, or even sequential consistency, when reading or writing fields of shared objects. This allows for compiler optimizations (such as register allocation, common subexpression elimination, and redundant read elimination), through reordering memory read-writes.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Java concurrency

Start with the simplest possible case. Write down what Java concurrency claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Java concurrency before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Java concurrency ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Java concurrency

In research
Java concurrency appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Java concurrency in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Java concurrency is common in secondary-school and first-year university syllabi. It links to neighbouring topics Concurrent computing, Java (programming language), Sun Microsystems, so understanding it makes those chapters shorter.
In everyday life
Look for Java concurrency outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Java concurrency” →

How to study Java concurrency in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Java concurrency means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Java concurrency out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Java concurrency in simple terms?

The Java programming language and the Java virtual machine (JVM) are designed to support concurrent programming. All execution takes place in the context of threads.

Why does Java concurrency matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Java concurrency?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Java concurrency.

Tags

  • Concurrent computing
  • Java (programming language)
  • Sun Microsystems

Keep exploring