Interview Q&A · Java · 7 questions in this topic
Processes, Threads and Multitasking — Java Interview Q&A
How processes differ from threads, what multitasking means, and why Java programs use threads. Part of Threads & Concurrency · 294 questions in the Java bank.
What is a process?
Answer
A process is a program in execution that is managed by the operating system. Each process receives its own private memory space for code, data, and resources such as open files and sockets. Because of this isolation, one process cannot directly read or corrupt the memory of another process. That safety makes processes heavyweight, since creating them and switching between them is relatively expensive.
What is a thread?
Answer
A thread is an independent path of execution that runs inside a process. All threads of a process share the same memory and resources, so they can communicate by reading and writing shared objects. Each thread has only its own stack and program counter, which makes threads lightweight compared to processes. The trade-off is that shared memory must be coordinated carefully to avoid race conditions.
Process vs thread?
Answer
A process owns a separate address space, while all threads of a process share the same address space. Processes are isolated and heavyweight, whereas threads are lightweight and start and switch faster. Threads can communicate cheaply through shared memory, while processes need slower mechanisms such as pipes or sockets. For this reason, threads suit fine-grained concurrent work inside one application, and processes suit isolation between independent programs.
Key points
- 1Weight: processes are heavyweight, threads are lightweight.
- 2Address space: each process has its own space, threads of a process share one space.
- 3Communication: threads share memory directly, processes need inter-process communication.
- 4Context switch: switching processes costs more than switching threads of the same process.
What is multitasking?
Answer
Multitasking means carrying out multiple activities during the same period of time. On a single core the system switches rapidly between tasks to create the appearance of simultaneity, while on multicore hardware tasks can genuinely run in parallel. The goal is better utilisation of the CPU and better responsiveness for the user. A familiar example is editing a document while a file downloads in the background.
Types of multitasking?
Answer
The two classic kinds are process-based and thread-based multitasking. Process-based multitasking runs several independent programs at once, with each program in its own address space. Thread-based multitasking runs several threads inside one program, sharing the same address space. Java is built for thread-based multitasking through the Thread class and the java.util.concurrent package.
Key points
- 1Process-based: several independent programs run at once, each isolated.
- 2Thread-based: several threads run inside one program and share memory.
- 3Java focuses on thread-based multitasking.
Benefits of multithreaded programming?
Answer
Multithreaded programs can use CPU time that would otherwise sit idle while one task waits for I/O. They stay responsive because urgent work, such as handling user input, can run on a separate thread from long computations. They also improve throughput on multicore machines by splitting work across cores. The price is greater complexity, because shared state must be synchronised to prevent races and deadlocks.
Key points
- 1Utilisation: threads keep the CPU busy during blocking I/O.
- 2Responsiveness: interactive work stays smooth while background work continues.
- 3Throughput: work can be spread across multiple cores.
- 4Cost: shared mutable state needs synchronization, which adds complexity and deadlock risk.
Explain a thread in Java.
Answer
In Java a thread is represented by a java.lang.Thread object that executes code concurrently with other threads. All application threads share the same heap, while each thread has its own stack for local variables and method calls. Every application starts with a main thread that runs the main method and can create child threads. Modern code usually submits tasks to an ExecutorService instead of creating raw threads, and Java 21 added virtual threads as a lightweight alternative for blocking tasks.
💡 Interview tip: mention virtual threads from Java 21 as the modern way to run many blocking tasks cheaply.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission