Menu
Topics Index
...
`

What is the output


If you need explanation Read this topic

If you need Answer Take test on this topic

What is the output
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);
        thread1.start();
        thread2.start();
    }
}

A. The program generates Compilation Error
B. The program compiles and execute without any Error
C. The program compiles without Error but deadlock occurs at runtime
D. The program compiles without error but throws runtime execeptions
E. None of the above
Topic: What is Multitasking and Multithreading?

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is B the program wil compile n executes with any problem and prints OP as

Shared resourse1 acquired by thread0
Shared resourse1 acquired by thread0
Shared resourse1 acquired by thread1
Shared resourse1 acquired by thread1

synchronized blocks wont allow to execute two thread at a time..one thread shud wait until others completes its execution

Posted by Uday Kumar    2014-07-01 13:11:52


ans b.
thread 1 .start() prints
shared resource1 acquired by Thread0
shared resource2 acquired by Thread0
thread2.strt() prints
shared resources1 acquired by Thread 1
shared resorce2 acquired by Thread 1

Posted by Maheshwari Natarajan    2014-07-01 13:14:46


Answer b.
whn a thread is insid synchronizd method ,al othr threads try to cal it on the same instance ve to wait.
currnt thread procsd thn nxt waitin thread accsd.
output:
thread1.start() tis method prints
shared resource1 acquired by Thread-0
shared resource2 acquired by Thread-0
thread2.start() tis method prints
shared resource1 acquired by Thtead-1
shared resource2 acquired by Thread-1

Posted by NagaRaj Nataraj    2014-07-01 13:28:02


b

Posted by Rutvik Gosalia    2014-07-01 13:38:40


B

Posted by Sarang Pathak    2014-07-01 13:45:10


B

Posted by Shri Nandhitha    2014-07-01 13:57:16


Congratulations Uday Kumar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-02 04:18:48


© meritcampus 2019

All Rights Reserved.

Open In App