A multithreaded program contains two or more parts that can run concurrently and each part can handle different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.
By definition multitasking is when multiple processes share common processing resources such as a CPU. Multithreading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application.
Multithreading enables you to write in a way where multiple activities can proceed concurrently in the same program.
Related Tags
Tutorials
A CompletableFuture is a Future that runs the next step when its result arrives, so no thread has to wait for it. This tutorial creates, chains and combines CompletableFuture tasks on virtual threads, handles errors and timeouts, and shows the mistakes that make async code hang or lose exceptions.
Executors.newVirtualThreadPerTaskExecutor() runs each task in a new virtual thread. Learn close(), invokeAll, Semaphore limits, thread names and errors.
Capture a Java thread dump with jcmd, jstack, kill -3 or JSON for virtual threads, read every line and find deadlocks, CPU hogs and stuck pools.
Java scoped values are final in Java 25. Learn ScopedValue.where().run(), call(), rebinding, child threads and how scoped values compare with ThreadLocal.
The Java volatile keyword makes writes visible to other threads but does not make count++ atomic. See happens-before, stop flags and lazy singletons.
Are local variables thread safe in Java? Yes, each thread keeps its own stack copy. See when shared objects, lambdas and escaping references break it.
Runnable vs Callable in Java explained with examples of return values, checked exceptions, Future, FutureTask, invokeAll and the Java 19 Future methods.
Learn what a daemon thread in Java is, how setDaemon() works, why the JVM does not wait for it, virtual threads, executors and when to use one.
How to handle InterruptedException in Java correctly. Propagate it or restore the interrupt flag, avoid common mistakes, and cancel executor tasks.
Semaphore vs ReentrantLock in Java compared with a table and code for ownership, reentrancy, IllegalMonitorStateException, conditions and when to use each.
IllegalMonitorStateException in Java explained, with every cause from wait() and notify() outside synchronized to ReentrantLock unlock() and the fixes.
Java thread join() waits for another thread to finish. Learn join(long), join(Duration), interrupts, deadlocks, visibility and yield() vs join().
Java concurrency explained for Java 25 with thread safety, a race condition fix, a timeline from Java 1.0 to 25 and all our tutorials grouped by task.
Learn to create and start a thread in Java with Runnable, Thread, Thread.ofPlatform(), virtual threads, ExecutorService, ThreadFactory and priority.
How to kill a thread in Java safely with interrupt() or a volatile flag, why Thread.stop() throws UnsupportedOperationException and how to stop tasks.
Set and get the thread name in Java with getName(), setName(), constructors and Thread builders, default names, executor thread names and threadId().
Java wait for all threads to finish with ExecutorService close(), invokeAll(), Future.get(), CountDownLatch, CompletableFuture.allOf() and join().
Java structured concurrency with the Java 25 preview API. Learn StructuredTaskScope.open(), fork, join, Joiners, timeouts, custom joiners and the flags.
Java virtual threads explained for Java 25 with examples, carrier threads, the Java 24 pinning fix, when not to use them, pooling and ThreadLocal pitfalls.
Java DelayQueue explained with a Delayed record, take(), poll() and peek() rules, a producer-consumer order timeout example and the alternatives.
Shut down a Java ExecutorService with shutdown(), awaitTermination() and shutdownNow(), or with try-with-resources and close() on Java 19+. Covers queued tasks, RejectedExecutionException, virtual threads, JVM exit and shutdown hooks.
Learn to cancel a task in ExecutorService with Future.cancel(), stop running tasks with interrupts, cancel after a timeout and clean up with purge().
Java ScheduledExecutorService examples for delayed and periodic tasks, scheduleAtFixedRate vs scheduleWithFixedDelay, exceptions, cancel and daily jobs.
Learn how ExecutorService invokeAll() runs a batch of Callable tasks, with timeouts, failed tasks, Future.state() and invokeAny() for the first result.
A RejectedExecutionHandler decides what happens to a task that a ThreadPoolExecutor cannot accept. Compare the four built-in policies with real output and write custom handlers.
Learn ExecutorService in Java with Java 25 examples for submit(), invokeAll(), Future results, close() with try-with-resources and virtual threads.
AtomicInteger lets many threads update one int without a lock. Covers every method with results, compare-and-swap, AtomicInteger vs synchronized vs volatile, and LongAdder for high contention.
Solve the producer consumer problem in Java with BlockingQueue, put() and take(), a poison pill per consumer, timeouts and virtual threads.
Learn the thread life cycle in Java with all six Thread.State values, a state diagram, runnable demos, transitions, virtual threads and thread dumps.
Concurrency vs parallelism explained with Java 25 examples, a timeline diagram, the four combinations and which Java API fits waiting or CPU-bound tasks.
Callable and Future in Java with runnable examples of get() with timeout, exceptions, cancel(), Java 19 state() and resultNow(), and FutureTask.
Java thread pool guide with Executors factory pools, a fixed size pool example, ThreadPoolExecutor settings, pool sizing, monitoring and virtual threads.
Learn how Java ConcurrentLinkedDeque works, with lock-free methods for both ends, a 100-thread example, size() limits, work stealing and deque comparison.
Java Semaphore explained with examples for acquire(), release(), tryAcquire() timeouts, fairness, throttling an executor and limiting virtual threads.
Java wait notify explained with wait(), notify() and notifyAll(), a bounded producer consumer queue, spurious wakeups and BlockingQueue.
A Java binary semaphore is a Semaphore(1). Learn how it works, when to use it, how it differs from a mutex, signaling and the over-release bug.
Use a Java Lock to let one thread at a time change shared data. Covers ReentrantLock with try-finally, tryLock() with a timeout, fair locks, ReadWriteLock, StampedLock, Condition and virtual threads.
Learn when to use Java ThreadLocal, why it leaks in thread pools, how remove() in finally fixes it, and when Java 25 ScopedValue is the better choice.
Use Java UncaughtExceptionHandler to log thread failures, restart threads with a limit, and see why ExecutorService submit() never reaches the handler.
Compare and swap (CAS) writes a new value only if a variable still holds the value we read. Learn compareAndSet(), CAS loops, VarHandle and the ABA problem in Java 25.
Java CountDownLatch example with await(), countDown() in finally, timeouts, a starting gate for tests and a comparison with CyclicBarrier and invokeAll.
Learn how ConcurrentHashMap works since Java 8, with atomic merge and compute methods, caching, bulk operations, newKeySet, sizing and common mistakes.
Runnable vs Thread in Java compared with examples, a difference table, shared tasks, executors and virtual threads, and when extending Thread still fits.
Sleep vs wait in Java compared with Java 25 examples, covering which method keeps the lock, the thread states, wake-up rules and modern alternatives.
Object level lock vs class level lock in Java with runnable proof that the two locks are independent, syntax variants, pitfalls and virtual thread notes.
See how the ThreadPoolExecutor BlockingQueue decides when the pool grows, with SynchronousQueue, bounded and priority queues, sizing and rejected tasks.
Deadlock in Java explained with a deterministic example, jstack and ThreadMXBean detection, lock ordering, tryLock timeouts and interruptible recovery.