Menu
Topics Index
...
`

Synchornization


If you need explanation Read this topic

Synchornization
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);
        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() {
        synchronized (target) {
            target.call(msg);
        }
    }
}

A. [Hello][Synshronized]
B. [Hello[Synshronized]]
C. Output can not be determined
D. Compilation Error or Runtime Error
Topic: Java Synchronized Blocks

If you need explanation Read this topic

User comments below. All of them might not be correct.

Ans is D,, Callme class is not extend in caller class,, so cant create a callme object..

Posted by Raju VarshnEy    2014-11-10 08:35:33


Answer is A .....This program demonstrates the usage if synchronised keyword specially when there is a shared data that is likely to be manipulated by multiplre threads at atime......Main method creates an instance of Callme class and two instances of Caller classes.msg is the variable that is being shared by multiple threads at time...here are the instances of caller are threads because they Caller implements Runnable and so every instance of callable is a thread....coming to program,new Caller(target,"msg") actually invokes the  constructor first where execution of a thread is started using t.start()....so,initially obj1 object synchronises target object and calls call method on target object and prints [hello]...when obj1 executes synchronised block, it means obj1 thread attained lock on target object and obj2 will be blocked(even though both threads are spawned by that time) until the obj1 comes out of synchronized block which is when lock will be released from the obj1 thread ...........                                    After obj1 executes the synchronised block lock is attained by the obj2 thread  and call method is now called with arguments synchronised as message and it prints [synshronised].

Posted by Shashanka Mogaliraju    2014-11-10 08:55:28


a

Posted by Shubham Bansal    2014-11-10 09:26:42


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
simultaneously are known as "critical sections." Java provides the synchronized statement to protect these
critical sections.The same keyword can also be used as a modifier for class or
instance methods. It indicates that the method modifies the internal state of the class or the
internal state of an instance of the class in a way that is not thread-safe. Before running a
synchronized class method, Java obtains a lock on the class, to ensure that no other threads
can modif the class concurrently. Before running a synchronized instance method, Java.
obtains a lock on the instance that invoked the method, ensuring that no other thread can modify
the object at the same time.but here we cant determine which thread is going to execute 1st it depend on the process time.call method executed with 2 thread.we get following 2 types of result [Hello][Synchronized]and [Synchronized][Hello] it can not be determined

Posted by Maheshwari Natarajan    2014-11-10 18:20:02


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 Ankur Verma. 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:04:10


© meritcampus 2019

All Rights Reserved.

Open In App