Junior — Middle
Is it possible to implement a multithreaded program that runs within a single process?
sobes.tech AI
Answer from AI
Yes, in Java it is possible to implement a multithreaded program that runs within a single process. In Java, multithreading is achieved by creating and managing threads (Thread) within a single JVM process. Each thread runs in parallel, sharing the process memory, which allows for efficient resource utilization and concurrent task execution.
Example of creating and starting two threads:
public class MultiThreadExample {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
System.out.println("Thread 1 is running");
});
Thread thread2 = new Thread(() -> {
System.out.println("Thread 2 is running");
});
thread1.start();
thread2.start();
}
}