Menu
Topics Index
...
`

Static Synchronized


If you need explanation Read this topic

If you need Answer Take test on this topic

Static Synchronized
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 {
    static 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.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.

Answer is A....In the for loop of the main we are creating 2 threads and we are setting the names of the threads with names person-1 and person-2 and in the first iteration of the for loop we are calling start method and so person-1 thread will execute  in a new thread not in main thread....static synchronised methods take locks at the class level and hence no other thread can actually invoke the same static method until the thread in execution completes the method and release the class level lock.....Here,though the person-1 thread sleeps for 0.4 seconds Person-2 thread does not enter the userAction method until person-1 method executes that method...so after person-1 prints person-1 logout and '"---------------" execution completes and person-2 thread then enters the userAction method

Posted by Shashanka Mogaliraju    2014-11-10 12:55:12


a

Posted by Shubham Bansal    2014-11-10 12:59:57


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-10 18:49:43


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 Ramya Reddy Addulla. 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:15:36


© meritcampus 2019

All Rights Reserved.

Open In App