Menu
Question Index
...

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();
    }
}


Person-1 login.
Work Started.
Person-1 logout.
----------------
Person-2 login.
Work Started.
Person-2 logout.
----------------
Person-1 login.
Person-2 login.
Work Started.
Work Started.
Person-1 logout.
----------------
Person-2 logout.
----------------
Person-2 login.
Work Started.
Person-1 login.
Work Started.
Person-2 logout.
----------------
Person-1 logout.
----------------
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: static synchronized In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App