Menu
Topics Index
...
`

Synchornization2979


If you need explanation Read this topic

If you need Answer Take test on this topic

Synchornization2979
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 {
    synchronized void call(String msg) {
        System.out.print("[" + msg);
        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.

A. [Hello][Synshronized]
Because call method is written with ' synchronized ' keyword. The method having written synchronized, works as a critical section in which at one time only one instance of class could execute.

Posted by Vijay Raichura    2015-01-21 03:52:11


ans is A..here we have the synchronization concept which means if write a method as synchronized than no threads can not use that Object at a time..if one thread using that resource than other threads must wait until it completes work..
Execution:

here we Caller class as thread class and CallMe class as resource...here we are creating the one instance for the CallMe class and creating 2 instances for the Caller class which are threads...these threads are using the single object(CallMe Object) so 1st object one will completes its execution next thread 2 will get chance to execute...so Hello and Synchronized get printed

Posted by Uday Kumar    2015-01-21 04:02:51


Ans is Option A... Execution starts from main() method after execution of first stmt it will create object of Class Callme with variable "target" after that it will create object of Class Caller... In Class Caller we have parametric constructor with Object of Callme and string as parameters so while creating object it will call that parametric constructor inside that constructor we are initializing member variables of Caller class as target=targ it will assign object of Callme and msg="Hello" and its creating the object of thread class by passing object of Caller class as parameter                 after that we are calling start() method using thread object... whenever we call the start() method using thread object it will calls the run() method so it will call run() method inside run() method we are calling the method call(msg) of Class Callme and call() method is synchronized block                                                                                                                                                if we write a method as synchronized then "it will allow  only one thread at a time"  All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.....      so first it will completely execute first object  (obj1)  and then it will go to second object (obj2) so first it will print [Hello] and then [Synshronized] so Ans is Option A...[Hello][Synshronized]

Posted by Bhagi Bhagyasri    2015-01-21 04:22:39


a

Posted by Shubham Bansal    2015-01-21 07:23:06


i'm going with option A... here key is call method is synchronized .. So.. Only one Thread can access that method at a single point of time ... So.. 1st obj1 calls "target's call method " and prints  [Hello] and then after the release of the current execution only.. obj2 's thread access the target's call method and results in  [Synchronized] ... So , Ans is  [Hello][Synchronized]..

Posted by ?????????? ?????    2015-01-21 09:43:23


This dose is now closed and the winners are Vijay Raichura,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. 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    2015-01-22 08:10:47


© meritcampus 2019

All Rights Reserved.

Open In App