Topics Index

Threads and Runnable interface1696

If you need explanation Read this topic

If you need Answer Take test on this topic

Threads and Runnable interface1696
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");
    public void run() {; }
    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 {
        } catch (Exception e) { }
    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 {
        } catch (Exception e) {    }
    synchronized void bMethod() {System.out.println("b method"); }

A. Compiles and executes successfully
B. Dead lock occurs
C. Compilation Error
D. 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.

answer a
run() methd foo(a) s cald using myclas b objct it use the main() as curnt methd
start() methd hre thread is hvin methid name"Main"
output ptints.
main in A
Main in B

Posted by NagaRaj Nataraj    2014-07-04 13:53:52

Ans is A.
Main in A
Main in B
succesfully completed
a method...

In main object of saina will call constructor in ehich mainthread is started and foo() method of MyclassA is called in that method "Main in A" will get printed...then run()method will get executed in which foo() method of MyClassB is called which prints "Main in B"....then next statement in main will get printed "Successfully completd" as there is sleep in class MyClassB so synchronized aMethod() will be called which prints "a method"....

Posted by Mânïshå Mùlchåndânï    2014-07-04 13:57:17

ans b.
deadlock occurs.
bcz 2 threads r circular dependency on a pair of synchronizd objct.

in tis progrm a.amethod() and b.b method() both r try to cal method in othr class.
so deadlock occurs
executn s nt completd.
it prints
main in A
Main in B
aft tat deadlock occur

Posted by Maheshwari Natarajan    2014-07-04 14:25:44

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

Posted by Merit Campus    2014-07-07 05:23:32

© meritcampus 2019

All Rights Reserved.

Open In App