Question Index

What is the output of the program?

class DeadlockOnDemo extends Thread {
    private final SharedResourceDetails r1;
    private final SharedResourceDetails r2;
    public DeadlockOnDemo(SharedResourceDetails r1, SharedResourceDetails r2) {
        this.r1 = r1;
        this.r2 = r2;
    public void run() {
        synchronized (r1) {
                System.out.println("Shared Resource1 acquired by " + getName());
        synchronized (r2) {
            System.out.println("Shared Resource2 acquired by " + getName());
class SharedResourceDetails {
    public SharedResourceDetails(String resourceName) {}
public class Concurency {
    public static void main(String[] args) {
        SharedResourceDetails r1 = new SharedResourceDetails("SharedResource1");
        SharedResourceDetails r2 = new SharedResourceDetails("SharedResource2");
        DeadlockOnDemo thread1 = new DeadlockOnDemo(r1, r2);
        DeadlockOnDemo thread2 = new DeadlockOnDemo(r1, r2);

The program generates Compilation Error
The program compiles and execute without any Error
The program compiles without Error but deadlock occurs at runtime
The program compiles without error but throws runtime execeptions
None of the above



Topic: What is Multitasking and Multithreading?

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App