Menu
Question Index
...

What is the output of the following program?

public class Saina implements Runnable {
    MyClassA a = new MyClassA();
    MyClassB b = new MyClassB();
    public Saina() {
        Thread mainThread = new Thread(this, "Main");
        mainThread.start();
        a.foo(b);
    }
    public void run() {b.foo(a); }
    public static void main(String arg[]) {
        new Saina();
        System.out.println("Successfully Completed");
    }
}
class MyClassA {
    synchronized void foo(MyClassB b) {
        String name = Thread.currentThread().getName();
        System.out.println(name + " in A");
        try {
            Thread.sleep(100);
        } catch (Exception e) { }
        b.bMethod();
    }
    synchronized void aMethod() {System.out.println("a method"); }
}
class MyClassB {
    synchronized void foo(MyClassA a) {
        String name = Thread.currentThread().getName();
        System.out.println(name + " in B");
        try {
            Thread.sleep(100);
        } catch (Exception e) {    }
        a.aMethod();
    }
    synchronized void bMethod() {System.out.println("b method"); }
}


Compiles and executes successfully
Dead lock occurs
Compilation Error
Runtime Error

Doubts

Problems

Topic: What is Multitasking and Multithreading?

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App