Unsynchronized Multi-threaded Printing
Consider the following code. What is the most likely outcome related to the output produced by this multi-threaded program?
public class InterleavedPrint {
public static void main(String[] args) {
Runnable printTask = () -> {
for(int i = 0; i < 5; i++) {
System.out.print(Thread.currentThread().getName() + " ");
}
};
new Thread(printTask, "Thread1").start();
new Thread(printTask, "Thread2").start();
}
}
A
The output may be interleaved due to unsynchronized printing from multiple threads.
B
The code will result in a deadlock because both threads contend for the print statement.
C
A race condition will cause the program to crash unexpectedly.
D
The threads will execute in a strictly sequential order based on the system scheduler.
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | ravi.palepu | 1 | 1 | 0m 00s | 100 |
| #2 | 7017046 | 0 | 1 | 0m 00s | -10 |
| #3 | ansonjia1371 | 0 | 2 | 0m 00s | -20 |
Items per page:
10
1 – 3 of 3
APFIVE