Menu
Topics Index
...
`

Static Synchronized2919


If you need explanation Read this topic

If you need Answer Take test on this topic

Static Synchronized2919
What will be output of the following program?
public class Employee {
    public static void main(String[] args) {
        for (int i = 1; i < 3; i++) {
            CheckingSpot t = new CheckingSpot();
            t.setName("Person-" + i);
            t.start();
        }
    }
}
class Office {
    synchronized void userAction() {
        System.out.println(Thread.currentThread().getName() + " login.");
        System.out.println("Work Started.");
        try {
            Thread.sleep(400);
        } catch (Exception e) {
            System.out.println("Thread Interrupted.");
        }
        System.out.println(Thread.currentThread().getName() + " logout.");
        System.out.println("----------------");
    }
}
class CheckingSpot extends Thread {
    public void run() {
        Office o = new Office();
        o.userAction();
    }
}

A. Person-1 login.
Work Started.
Person-1 logout.
----------------
Person-2 login.
Work Started.
Person-2 logout.
----------------
B. Person-1 login.
Person-2 login.
Work Started.
Work Started.
Person-1 logout.
----------------
Person-2 logout.
----------------
C. Person-2 login.
Work Started.
Person-1 login.
Work Started.
Person-2 logout.
----------------
Person-1 logout.
----------------
D. Output can not be determined
E. Compilation Error or Runtime Error
Topic: static synchronized In Java

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.

ans is not here.. correct answer is      Person-1login. Work Started. Person-2login. Work Started. Person-1logout. ---------------- Person-2logout. ----------------

Posted by Shubham Bansal    2014-11-11 12:30:17


Ans is D....here we use the Concept of the Synchronized which means when we are working with the mutliple threads we may need to take care...when two threads need to acces single resource there may be a chance of getting Conflicts some times...We use Synchronized keyword to avoid this Conflicts....when we make an method Synchronized than no thwo threads cant access the resource at a time...if on e thread is working than other thread should wait untill it completes it work or until it notifies....

here we create 3 threads and set the names like Person 1,Person2 etc...

here these 3 threads are using the single Object of the Office class...in Office we have method userAction() which is Synchronized...than more than 1 thread cant access this method at a time....here threds slicing depends on the Processor so that Op cant be determined....

Posted by Uday Kumar    2014-11-11 12:48:52


ans d. output can not be determined.in this program we hav 3 classes Employee is main class ,.checkingspot extends office class .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. coming to this program for loop generates 2 threads,start() method invokes run(),run() executes UserAction().here we can not determine which thread is goig to execute 1st and 2nd.its all depends on process execution time

Posted by Maheshwari Natarajan    2014-11-11 15:58:44


Answer is d.....we cannot determine because of parallel execution of threads......
main thread creates two instances of CheckingSpots.Since CheckingSpot class extends Thread,every instance of CheckingSpot class is also a Thread..
WE are naming the threads using setName method....

t.start in the main method for the first iteration will actually invoke a new Thread Person-1  in which a new office object is created and useraction method is called which is synchronised...

Whenever synchronised non static method is executed on an object,We actually lock on the objectwhich is calling the method and  no other thread actually invoke any of remaining methods on the same object....

MeanWhile 2nd thread which is Person-2 thread will also spawn simultaneously and executes run method and create new Office object....
point to note is here office is not a shared resource as we are creating 2 new instances of office in each threads...

synchronised method execution of these 2 threads happens simultaneously because these 2 threads take locks on two different objects and so the useraction method execution takes place concurrently.....

However we could not guarantee the execution of 2 threads as to which will be invoked in what order as the priority of both these threads is same (the default priority)...Hence concurrent execution of these two threads does not guarantee a constant output

Posted by Shashanka Mogaliraju    2014-11-11 18:47:51


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan, Shashanka Mogaliraju,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. 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-12 04:30:40


© meritcampus 2019

All Rights Reserved.

Open In App