Runtime.getRuntime().exec() → executes system commands.
ProcessBuilder class → more advanced control over process creation and I/O.
import java.io.IOException;
public class ProcessExample
{
public static void main(String[] args) throws IOException
{
Process process = Runtime.getRuntime().exec("notepad.exe"); // opens Notepad
}
}
Thread class
Runnable interface
| Aspect | Process | Thread |
|---|---|---|
| Definition | A process is a program in execution. | A thread is the smallest unit of execution within a process. |
| Memory | Each process has its own memory space. | Threads share the memory of the process they belong to. |
| Overhead | Processes are heavier and require more resources. | Threads are lighter and more efficient in context switching. |
| Isolation | Processes are isolated; one process cannot directly access another’s memory. | Threads are not isolated; they share data within the same process. |
| Communication | Inter-Process Communication (IPC) is slower. | Communication between threads is faster since they share memory. |
| Creation in Java | Created using Runtime.getRuntime().exec() or ProcessBuilder. |
Created by extending Thread or implementing Runnable. |
| Failure | If a process crashes, it does not affect other processes. | If a thread crashes, it can bring down the whole process. |
| Basic Unit | Process is the basic unit of resource allocation. | Thread is the basic unit of execution. |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.