Menu
Topics Index
...
`

Threads


If you need explanation Read this topic

If you need Answer Take test on this topic

Threads
What is the output of the following program?
public class Kohli {
    public static void main(String... n) {
        Resource r1 = new Resource("Res 1");
        Resource r2 = new Resource("Res 2");
        DeadLockDemo demo1 = new DeadLockDemo(r1, r2);
        DeadLockDemo demo2 = new DeadLockDemo(r2, r1);
        demo2.start();
        demo1.start();
    }
}
class DeadLockDemo extends Thread {
    Resource resource1;
    private final Resource resource2;
    public DeadLockDemo(Resource resource1, Resource resource2) {
        this.resource1 = resource1;
        this.resource2 = resource2;
    }
    public void run() {
        try {
            resource1.finalize();
            resource2.finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        synchronized (resource1) {
            System.out.println(resource1.getResourceName() + " ");
        }
        resource1 = null;
        synchronized (resource2) {
            System.out.println(resource2.getResourceName() + " ");
        }
    }
}
class Resource extends Object {
    private final String resourceName;
    public Resource(String resourceName) {
        this.resourceName = resourceName;
    }
    public String getResourceName() { return resourceName;  }
    protected void finalize() throws Throwable {super.finalize(); }
}

A. Compiles and executes successfully with some output
B. Compiles and executes successfully without any output
C. Compiles but deadlock occurs at run time
D. Compilation Error
E. Runtime Error
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 A: since the resources are synchronized there is no chance of dead lock

Posted by Manasa Jyothi Sree    2014-03-18 12:58:48


Ans is B... finalize method is used to release the resource. so resources will be destroyed before print statement.. so prgrm compiles successfully and returns no output..

Posted by Om Prakash Om    2014-03-18 13:09:32


A.compiles and xecutes fine...with some output such as
res2
res1
res1
res2

Posted by Shabaaz Shaikz    2014-03-18 13:14:35


Ans A. Compiles and executes successfully with output as
Res 2
Res 1
Res 2
Res 1
There is no chance of deadlock since the Threads releases their resources after completing their execution.

Posted by Nikhil Sulibhavi    2014-03-18 13:29:32


Ans A. Compiles and executes successfully with output as
Res 2
Res 1
Res 2
Res 1
There is no chance of deadlock since the Threads releases their resources after completing their execution.

Posted by Akhil Goutham Kotini    2014-03-18 14:12:13


Congratulations Manasa Jyothi Sree. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-19 04:25:00


© meritcampus 2019

All Rights Reserved.

Open In App