Java · OOP, JVM and backend craft
What are the basics of concurrency in Java?
Short answer
Concurrency lets multiple threads make progress on shared work. The hard parts are atomicity, visibility and ordering, which must be handled whenever mutable state is shared.
A thread is an independent path of execution with its own stack but shared heap. Threads improve throughput for blocking IO and use multiple cores for CPU work. They also introduce races when they read and write shared mutable state without coordination.
public class Counter {
private int value;
public void increment() {
value++;
}
public int value() {
return value;
}
}
Runnable task = () -> {
for (int i = 0; i < 10_000; i++) {
counter.increment();
}
};The three failure modes
- Atomicity: a compound action can be interrupted halfway
- Visibility: one thread may not see another thread's write
- Ordering: the compiler or CPU may reorder independent operations
- Deadlock: threads wait on each other's locks forever
Prefer high-level tools over raw threads: executor services, concurrent collections, atomics and synchronisers. Confine mutable state to one thread where possible. Use start to launch a thread and run only to execute its body on the current thread.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between start and run on a Thread?
- 2Why is value++ not atomic?
- 3What is a deadlock and how do you avoid it?
- 4When is immutability better than locking?
Say it in the interview
Concurrency means multiple threads share the heap but have their own stacks. The problems are atomicity, visibility and ordering, plus deadlock. A simple increment is three operations, so two threads can lose updates. I prefer executors, atomics and concurrent collections over raw threads, and I reduce shared mutable state through confinement and immutability, because that removes whole classes of bugs.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Java mock interview and get scored feedback on depth, clarity and confidence.