Menu
Topics Index
...
`

Synchornization2917


If you need explanation Read this topic

If you need Answer Take test on this topic

Synchornization2917
What will be the output of the following program?
public class SynchronizedExample {
    public static void main(String[] args) {
        Callme target = new Callme();
        Caller obj1 = new Caller(target, "Hello");
        Caller obj2 = new Caller(target, "Synshronized");
    }
}
class Callme {
    void call(String msg) {
        System.out.print("[" + msg);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }
        System.out.print("]");
    }
}
class Caller implements Runnable {
    String msg;
    Callme target;
    Thread t;
    public Caller(Callme targ, String s) {
        target = targ;
        msg = s;
        t = new Thread(this);
        t.start();
    }
    public void run() {
        target.call(msg);
    }
}

A. [Hello][Synshronized]
B. [Hello[Synshronized]]
C. Output can not be determined
D. Compilation Error or Runtime Error
Topic: Thread Synchronization In Java Using 'Synchronized'

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 is B.......Here when obj1 thread calls call method it prints [Hello and goes to sleep ...since the call to call method is not synchronised,obj2 also starts executing independently and prints [synchronised and then obj1 prints ] and obj2 prints ]

Posted by Shashanka Mogaliraju    2014-11-10 12:14:08


b

Posted by Shubham Bansal    2014-11-10 13:00:03


ans c.output can not be determined.in this program we hav 3 classes synchronized example is main class ,caller implements runnabable interface which supports multithreading..The Runnable interface must be implemented by any class that will initiate a separate thread
of execution. Runnable only defines one abstract method, called run( ), which is the entry
point to the thread.in main class objects for callme and caller are created.caller class default constructor get executed.t.start() method invoke the run() method here synchronized keyword is used. Since Java is a multithreaded system, care must often be taken to prevent multiple threads from modifying objects
simultaneously in a way that might leave the object's state corrupted. Sections of code that must not be executed.dafault caller constructor initialize al the object and new thread is created.t.start() invokes run(),run executes call method.here 1st sop printd and it wait for 1sec thn 2nd sop printd.here we can not determine which thread executes 1st and which is 2nd. here if thread havong hello executes 1st thn 2nd thread synchronized get executed at the waiting time of the 1st thread.so out cannot determined it depends on execution time.we can get any of the following 1 ans:[Hello[Synchronized]] or[Synchronized[Hello]]

Posted by Maheshwari Natarajan    2014-11-10 18:36:45


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Shashanka Mogaliraju,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Bahunadam Narendra. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-11 01:09:10


© meritcampus 2019

All Rights Reserved.

Open In App